diff --git a/src/storage/samplePackagesRepository.spec.ts b/src/storage/samplePackagesRepository.spec.ts new file mode 100644 index 0000000..b9fcff0 --- /dev/null +++ b/src/storage/samplePackagesRepository.spec.ts @@ -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, + }); + }); +}); diff --git a/src/storage/samplePackagesRepository.ts b/src/storage/samplePackagesRepository.ts new file mode 100644 index 0000000..7209a5e --- /dev/null +++ b/src/storage/samplePackagesRepository.ts @@ -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 => { + const storage = createKeyValueStorage(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)), + }; + }; diff --git a/src/storage/types.ts b/src/storage/types.ts index 57a7d45..9219ca6 100644 --- a/src/storage/types.ts +++ b/src/storage/types.ts @@ -1,5 +1,9 @@ +import { CarrierType, PackageStatusType } from '../types'; + +type RowInfo = { found: true; value: TValue } | { found: false }; + export type KeyValueStorage = { - get(key: TKey): Promise<{ found: true; value: TValue } | { found: false }>; + get(key: TKey): Promise>; set(key: TKey, value: TValue): Promise; }; @@ -9,3 +13,22 @@ export type ClearableKeyValueStorage = KeyValueStorage< > & { clear(): Promise; }; + +export type PackageInfo = { + trackingNumber: string; + carrier: CarrierType; + senderAddress: string; + receiverAddress: string; + articleName: string; + articleQuantity: number; + articlePrice: number; + SKU: string; + status: PackageStatusType; +}; + +export type PackagesRepository = { + getPackage( + carrier: CarrierType, + trackingNumber: string, + ): Promise>; +}; diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..111580e --- /dev/null +++ b/src/types.ts @@ -0,0 +1,7 @@ +export type CarrierType = 'DHL' | 'UPS' | 'DPD' | 'FedEx' | 'GLS'; +export type PackageStatusType = + | 'in-transit' + | 'inbound-scan' + | 'delivery' + | 'transit' + | 'scanned';