parent
5aba64eb5f
commit
2a531a19fe
@ -0,0 +1,29 @@ |
||||
import { createSamplePackagesRepository } from './samplePackagesRepository'; |
||||
|
||||
describe('createSamplePackagesRepository', () => { |
||||
it('returns correct info for FedEx/TN12345681', async () => { |
||||
const client = await createSamplePackagesRepository(); |
||||
const result = await client.getPackage('FedEx', 'TN12345681'); |
||||
expect(result).toEqual({ |
||||
found: true, |
||||
value: { |
||||
SKU: 'HP678', |
||||
articleName: 'Headphones', |
||||
articlePrice: 100, |
||||
articleQuantity: 1, |
||||
carrier: 'FedEx', |
||||
receiverAddress: 'Street 9, 1016 Amsterdam, Netherlands', |
||||
senderAddress: 'Street 4, 50667 Cologne, Germany', |
||||
status: 'transit', |
||||
trackingNumber: 'TN12345681', |
||||
}, |
||||
}); |
||||
}); |
||||
it('does not return info for DHL/TN12345681', async () => { |
||||
const client = await createSamplePackagesRepository(); |
||||
const result = await client.getPackage('DHL', 'TN12345681'); |
||||
expect(result).toEqual({ |
||||
found: false, |
||||
}); |
||||
}); |
||||
}); |
@ -0,0 +1,151 @@ |
||||
import { CarrierType } from '../types'; |
||||
import { createKeyValueStorage } from './inMemoryDB'; |
||||
import { PackageInfo, PackagesRepository } from './types'; |
||||
|
||||
const samplePackages = ( |
||||
[ |
||||
[ |
||||
'TN12345678', |
||||
'DHL', |
||||
'Street 1, 10115 Berlin, Germany', |
||||
'Street 10, 75001 Paris, France', |
||||
'Laptop', |
||||
1, |
||||
800, |
||||
'LP123', |
||||
'in-transit', |
||||
], |
||||
[ |
||||
'TN12345678', |
||||
'DHL', |
||||
'Street 1, 10115 Berlin, Germany', |
||||
'Street 10, 75001 Paris, France', |
||||
'Mouse', |
||||
1, |
||||
25, |
||||
'MO456', |
||||
'in-transit', |
||||
], |
||||
[ |
||||
'TN12345679', |
||||
'UPS', |
||||
'Street 2, 20144 Hamburg, Germany', |
||||
'Street 20, 1000 Brussels, Belgium', |
||||
'Monitor', |
||||
2, |
||||
200, |
||||
'MT789', |
||||
'inbound-scan', |
||||
], |
||||
[ |
||||
'TN12345680', |
||||
'DPD', |
||||
'Street 3, 80331 Munich, Germany', |
||||
'Street 5, 28013 Madrid, Spain', |
||||
'Keyboard', |
||||
1, |
||||
50, |
||||
'KB012', |
||||
'delivery', |
||||
], |
||||
[ |
||||
'TN12345680', |
||||
'DPD', |
||||
'Street 3, 80331 Munich, Germany', |
||||
'Street 5, 28013 Madrid, Spain', |
||||
'Mouse', |
||||
1, |
||||
25, |
||||
'MO456', |
||||
'delivery', |
||||
], |
||||
[ |
||||
'TN12345681', |
||||
'FedEx', |
||||
'Street 4, 50667 Cologne, Germany', |
||||
'Street 9, 1016 Amsterdam, Netherlands', |
||||
'Laptop', |
||||
1, |
||||
900, |
||||
'LP345', |
||||
'transit', |
||||
], |
||||
[ |
||||
'TN12345681', |
||||
'FedEx', |
||||
'Street 4, 50667 Cologne, Germany', |
||||
'Street 9, 1016 Amsterdam, Netherlands', |
||||
'Headphones', |
||||
1, |
||||
100, |
||||
'HP678', |
||||
'transit', |
||||
], |
||||
[ |
||||
'TN12345682', |
||||
'GLS', |
||||
'Street 5, 70173 Stuttgart, Germany', |
||||
'Street 15, 1050 Copenhagen, Denmark', |
||||
'Smartphone', |
||||
1, |
||||
500, |
||||
'SP901', |
||||
'scanned', |
||||
], |
||||
[ |
||||
'TN12345682', |
||||
'GLS', |
||||
'Street 5, 70173 Stuttgart, Germany', |
||||
'Street 15, 1050 Copenhagen, Denmark', |
||||
'Charger', |
||||
1, |
||||
20, |
||||
'CH234', |
||||
'scanned', |
||||
], |
||||
] as const |
||||
).map( |
||||
([ |
||||
trackingNumber, |
||||
carrier, |
||||
senderAddress, |
||||
receiverAddress, |
||||
articleName, |
||||
articleQuantity, |
||||
articlePrice, |
||||
SKU, |
||||
status, |
||||
]) => ({ |
||||
trackingNumber, |
||||
carrier, |
||||
senderAddress, |
||||
receiverAddress, |
||||
articleName, |
||||
articleQuantity, |
||||
articlePrice, |
||||
SKU, |
||||
status, |
||||
}), |
||||
); |
||||
|
||||
const getKey = (carrier: CarrierType, trackingNumber: string) => |
||||
`${carrier}/${trackingNumber}`; |
||||
|
||||
export const createSamplePackagesRepository = |
||||
async (): Promise<PackagesRepository> => { |
||||
const storage = createKeyValueStorage<PackageInfo>(50); |
||||
|
||||
await Promise.all( |
||||
samplePackages.map((samplePackage) => |
||||
storage.set( |
||||
getKey(samplePackage.carrier, samplePackage.trackingNumber), |
||||
samplePackage, |
||||
), |
||||
), |
||||
); |
||||
|
||||
return { |
||||
getPackage: (carrier: CarrierType, trackingNumber: string) => |
||||
storage.get(getKey(carrier, trackingNumber)), |
||||
}; |
||||
}; |
@ -0,0 +1,7 @@ |
||||
export type CarrierType = 'DHL' | 'UPS' | 'DPD' | 'FedEx' | 'GLS'; |
||||
export type PackageStatusType = |
||||
| 'in-transit' |
||||
| 'inbound-scan' |
||||
| 'delivery' |
||||
| 'transit' |
||||
| 'scanned'; |
Loading…
Reference in new issue