-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b2096c9
commit 20b7e9f
Showing
12 changed files
with
424 additions
and
70 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
quote_type = single |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Command } from 'commander'; | ||
import { setEnvValue } from '../../util/env.util'; | ||
import { UTC_TIME_FORMAT } from '../../config/constant'; | ||
import { info, log } from '../../util/console'; | ||
import { getGHNOrder, showOrders } from '../../services/ghn.service'; | ||
|
||
export const ghnCommand = (): Command => { | ||
const ghn = new Command('ghn').description('manage order, get information,...'); | ||
|
||
ghn | ||
.command('token') | ||
.description('Set GHN token') | ||
.option('-s, --set <token>', 'Save access token into .env') | ||
.action(async (options) => { | ||
try { | ||
if (options.set) { | ||
setEnvValue('GHN_TOKEN', options.set); | ||
} | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
}); | ||
|
||
ghn | ||
.command('get') | ||
.description('get order information') | ||
.option('-c, --code <value>', 'order code') | ||
.option('-d, --date <yyyy-MM-dd>', 'created date') | ||
.option('-f, --from <yyyy-MM-dd>', 'from date') | ||
.option('-t, --to <yyyy-MM-dd>', 'to date') | ||
.action(async (options) => { | ||
if (options.code) { | ||
const order = await getGHNOrder(options.code); | ||
|
||
if (order) { | ||
info(order); | ||
} else { | ||
info(`❌ Can not find order with code: ${options.code}`); | ||
} | ||
} | ||
|
||
if (options.date || options.from || options.to) { | ||
const { date, from, to } = options; | ||
let fromPurchaseDate; | ||
let toPurchaseDate; | ||
if (date) { | ||
fromPurchaseDate = toPurchaseDate = new Date( | ||
`${date}${UTC_TIME_FORMAT}` | ||
); | ||
} else { | ||
fromPurchaseDate = from | ||
? new Date(`${from}${UTC_TIME_FORMAT}`) | ||
: new Date(); | ||
toPurchaseDate = to | ||
? new Date(`${to}${UTC_TIME_FORMAT}`) | ||
: new Date(); | ||
} | ||
log(`From: ${fromPurchaseDate}, to: ${toPurchaseDate}`); | ||
await showOrders(fromPurchaseDate, toPurchaseDate); | ||
} | ||
}); | ||
|
||
return ghn; | ||
}; |
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
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
8 changes: 4 additions & 4 deletions
8
src/commands/vnpost.command.ts → src/commands/vnpost/vnpost.command.ts
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const ghn = { | ||
baseShipUrl: 'https://online-gateway.ghn.vn/shiip/public-api', | ||
baseOrderTrackingUrl: | ||
'https://online-gateway.ghn.vn/order-tracking/public-api', | ||
searchOrder: '/v2/shipping-order/search', | ||
getOrder: '/v2/shipping-order/detail', | ||
trackingLogs: '/client/tracking-logs', | ||
token: process.env.GHN_TOKEN, | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { info } from '../util/console'; | ||
import { Order } from '../dtos/order.dto'; | ||
import { Order as GHNOrder, getOrder as getGHNOrder, getTrackingLogs, searchOrder } from '../util/ghn.util'; | ||
|
||
const showOrders = async (fromPurchaseDate: Date, toPurchaseDate: Date) => { | ||
try { | ||
const orders: Order[] = await getOrders( | ||
fromPurchaseDate.toISOString(), | ||
toPurchaseDate.toISOString() | ||
); | ||
info( | ||
`🙌 Find ${ | ||
orders?.length | ||
} GHN orders from ${fromPurchaseDate.toLocaleDateString()} to ${toPurchaseDate.toLocaleDateString()}!` | ||
); | ||
|
||
orders.forEach(async (order, index) => { | ||
info( | ||
`-------------------- 🔰 GHN Order #${index + 1}: ${ | ||
order.code | ||
} 🔰 --------------------` | ||
); | ||
showOrder(order); | ||
}); | ||
|
||
info(`(Total: ${orders?.length} orders)`); | ||
} catch (error) { | ||
console.error(error.message); | ||
} | ||
}; | ||
|
||
const showOrder = (order: Order) => { | ||
info('• Order Id: ' + order.id); | ||
info('• Order Code: ' + order.code); | ||
info('• Order delivery status: ' + order.status); | ||
info('• Order products: ' + order.products); | ||
info('• Order delivery date: ' + order.doneAt); | ||
}; | ||
|
||
const getOrders = async ( | ||
fromDate: string, | ||
toDate: string | ||
): Promise<Order[]> => { | ||
return []; | ||
// const data: OrderRequestDto = { | ||
// ChildUserId: '', | ||
// CreateTimeStart: fromDate, | ||
// CreateTimeEnd: toDate, | ||
// KeySearch: '', | ||
// OrderByDescending: true, | ||
// PageIndex: 0, | ||
// PageSize: 1000, | ||
// }; | ||
|
||
// const orders: VNPostListOrder = await searchOrder(data); | ||
|
||
// return orders?.Items?.map((item: VNPostOrder) => { | ||
// const order = { | ||
// id: item.Id, | ||
// statusCode: item.OrderStatusId, | ||
// status: item.OrderStatusName, | ||
// fullName: item.ReceiverFullname, | ||
// phone: item.ReceiverTel, | ||
// codAmount: item.CodAmount, | ||
// feeShip: Number(item.TotalFreightIncludeVat), | ||
// products: item.PackageContent.substring( | ||
// 0, | ||
// item.PackageContent.indexOf('TMĐT') | ||
// ), | ||
// code: item.OrderCode, | ||
// createdAt: new Date(item.CreateTime), | ||
// doneAt: new Date(item.DeliveryTime), | ||
// }; | ||
|
||
// return order; | ||
// }); | ||
}; | ||
|
||
const getOrder = async (orderCode: string): Promise<Order> => { | ||
const order: GHNOrder = await getGHNOrder(orderCode); | ||
if (order && !order.returnFee) { | ||
const orderFromTrackingLogs = await getTrackingLogs(orderCode); | ||
|
||
order.statusName = orderFromTrackingLogs?.statusName; | ||
order.returnFee = orderFromTrackingLogs?.returnFee; | ||
order.mainServiceFee = orderFromTrackingLogs?.mainServiceFee; | ||
order.totalFee = orderFromTrackingLogs?.totalFee; | ||
} | ||
|
||
return { | ||
id: order.id, | ||
statusCode: undefined, | ||
status: order.status, | ||
fullName: order.toName, | ||
phone: order.toPhone, | ||
address: order.toAddress, | ||
codAmount: order.codAmount, | ||
feeShip: order.totalFee, | ||
products: order.items.map((p) => p.name).join(','), | ||
code: order.orderCode, | ||
createdAt: order.orderDate, | ||
doneAt: order.finishDate, | ||
returnAt: order.returnTime, | ||
} as Order; | ||
}; | ||
|
||
export { getOrder as getGHNOrder, showOrders }; |
Oops, something went wrong.