Created
June 2, 2020 08:17
-
-
Save olaoluwa-98/a38e62ac50b1c11a2f9e0be3b4250475 to your computer and use it in GitHub Desktop.
Kwik implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const KwikPackage = require("kwik-node"); | |
const ThirdPartyException = require("../../exceptions/ThirdPartyException"); | |
const KwikAPI = new KwikPackage(process.env.KWIK_DOMAIN_NAME, process.env.NODE_ENV == "production"); | |
class Kwik { | |
static async getToken() { | |
let { body: res } = await KwikAPI.adminLogin({ | |
email: process.env.KWIK_EMAIL, | |
password: process.env.KWIK_PASSWORD | |
}); | |
if (res.status != 200) throw new ThirdPartyException(res.message, res.data); | |
const { data } = res; | |
KwikAPI.setAccessToken(data.access_token); | |
KwikAPI.setVendorId(data.vendor_details.vendor_id); | |
KwikAPI.setUserId(data.vendor_details.user_id); | |
KwikAPI.setCardId(data.vendor_details.card_id); | |
} | |
static async getShippingCost({ catalogLocation, userLocation }) { | |
const deliveries = [ | |
{ | |
name: userLocation.fullname, | |
phone: userLocation.phone_no, | |
address: userLocation.street_address, | |
latitude: userLocation.latitude, | |
longitude: userLocation.longitude, | |
time: new Date().toISOString(), | |
has_return_task: false, | |
is_package_insured: 0 | |
} | |
]; | |
const pickups = [ | |
{ | |
name: catalogLocation.fullname, | |
email: catalogLocation.email, | |
phone: catalogLocation.phone_no, | |
address: catalogLocation.street_address, | |
latitude: catalogLocation.latitude, | |
longitude: catalogLocation.longitude, | |
time: new Date().toISOString() | |
} | |
]; | |
await Kwik.getToken(); | |
const payload = { | |
timezone: "+60", | |
custom_field_template: "pricing-template", | |
auto_assignment: 1, | |
layout_type: 1, | |
pickup_custom_field_template: "pricing-template", | |
has_pickup: 1, | |
has_delivery: 1, | |
is_multiple_tasks: 1, | |
payment_method: "32" /* payment via card */, | |
is_schedule_task: 1, | |
deliveries, | |
pickups | |
}; | |
try { | |
const { body: res } = await KwikAPI.getExactPricingForDeliveryTask(payload); | |
const { data, message, status } = res; | |
if (status >= 400) throw new ThirdPartyException(message, data); | |
const shipment_charge = deliveries.length * Number(data.per_task_cost) * 100; | |
const insurance_fee = Number(data.insurance_amount) * 100; | |
const service_charge = Number(data.total_service_charge) * 100; | |
const total_charge = shipment_charge + insurance_fee + service_charge; | |
return [ | |
{ | |
name: "Kwik", | |
shipment_charge, | |
currency: data.currency.code, | |
insurance_fee, | |
service_charge, | |
total_no_of_tasks: data.total_no_of_tasks, | |
total_charge, | |
min_delivery_time: null, | |
max_delivery_time: null | |
} | |
]; | |
} catch (error) { | |
if (error.name == "ThirdPartyException") throw error; | |
console.log(error); | |
if (error.response && error.response.data.status == "101") { | |
// retry the call | |
// await Kwik.getToken(); | |
// await Kwik.getShippingCost({ userLocation, catalogLocation }); | |
} | |
// handle errors 100 and 404 | |
throw new ThirdPartyException(error.response.data.message, error.response.data.data); | |
} | |
} | |
static async createShipping({ order, catalogLocation, userLocation }) { | |
await Kwik.getToken(); | |
const [result] = await Kwik.getShippingCost({ | |
catalogLocation, | |
userLocation | |
}); | |
console.log(result); | |
const { insurance_fee, service_charge, total_charge, total_no_of_tasks } = result; | |
const deliveries = [ | |
{ | |
name: userLocation.fullname, | |
phone: userLocation.phone_no, | |
address: userLocation.street_address, | |
latitude: userLocation.latitude, | |
longitude: userLocation.longitude, | |
time: new Date().toISOString(), | |
has_return_task: false, | |
is_package_insured: 0 | |
} | |
]; | |
const pickups = [ | |
{ | |
name: catalogLocation.fullname, | |
email: catalogLocation.email, | |
phone: catalogLocation.phone_no, | |
address: catalogLocation.street_address, | |
latitude: catalogLocation.latitude, | |
longitude: catalogLocation.longitude, | |
time: new Date().toISOString() | |
} | |
]; | |
const payload = { | |
timezone: "+60", | |
custom_field_template: "pricing-template", | |
auto_assignment: 1, | |
layout_type: 1, | |
pickup_custom_field_template: "pricing-template", | |
has_pickup: 1, | |
has_delivery: 1, | |
is_multiple_tasks: 1, | |
payment_method: "32" /* payment via card */, | |
is_schedule_task: 1, | |
insurance_amount: insurance_fee / 100, | |
amount: total_charge / 100, | |
total_no_of_tasks, | |
total_service_charge: service_charge / 100, | |
deliveries, | |
pickups | |
}; | |
try { | |
let { body: res } = await KwikAPI.scheduleDeliveryTask(payload); | |
console.log(res); | |
const { data, message, status } = res; | |
if (status >= 400) throw new ThirdPartyException(message, data); | |
const { | |
deliveries: [delivery], | |
pickups: [pickup] | |
} = data; | |
return { | |
response: data, | |
shipment: { | |
order_id: order.id, | |
shipment_id: delivery.job_hash, | |
currency: "NGN", | |
is_insured: pickup.auto_assignment_data.deliveries[0].is_package_insured, | |
insurance_fee, | |
courier_id: null, | |
courier_name: "Kwik", | |
shipping_cost: total_charge, | |
tracking_id: delivery.job_hash, | |
tracking_page_url: delivery.result_tracking_link, | |
pickup: false, | |
dropoff_url: null, | |
earliest_arrival: new Date(pickup.auto_assignment_data.deliveries[0].job_delivery_datetime), | |
latest_arrival: new Date(pickup.auto_assignment_data.deliveries[0].job_delivery_datetime), | |
shipping_provider: "kwik", | |
user_location_id: userLocation.id, | |
catalog_location_id: catalogLocation.id | |
} | |
}; | |
} catch (error) { | |
if (error.name == "ThirdPartyException") throw error; | |
console.log(error); | |
if (error.response.data.status == "101") { | |
// retry the call | |
// await Kwik.getToken(); | |
// await Kwik.createShipping({ userLocation, catalogLocation }); | |
} | |
// handle errors 100 and 404 | |
throw new ThirdPartyException(error.response.data.message, error.response.data.data); | |
} | |
} | |
} | |
module.exports = Kwik; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment