implemented sample packages repository

main
Inga 🏳‍🌈 1 year ago
parent 5aba64eb5f
commit 2a531a19fe
  1. 29
      src/storage/samplePackagesRepository.spec.ts
  2. 151
      src/storage/samplePackagesRepository.ts
  3. 25
      src/storage/types.ts
  4. 7
      src/types.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,
});
});
});

@ -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)),
};
};

@ -1,5 +1,9 @@
import { CarrierType, PackageStatusType } from '../types';
type RowInfo<TValue> = { found: true; value: TValue } | { found: false };
export type KeyValueStorage<TKey, TValue> = { export type KeyValueStorage<TKey, TValue> = {
get(key: TKey): Promise<{ found: true; value: TValue } | { found: false }>; get(key: TKey): Promise<RowInfo<TValue>>;
set(key: TKey, value: TValue): Promise<void>; set(key: TKey, value: TValue): Promise<void>;
}; };
@ -9,3 +13,22 @@ export type ClearableKeyValueStorage<TKey, TValue> = KeyValueStorage<
> & { > & {
clear(): Promise<void>; clear(): Promise<void>;
}; };
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<RowInfo<PackageInfo>>;
};

@ -0,0 +1,7 @@
export type CarrierType = 'DHL' | 'UPS' | 'DPD' | 'FedEx' | 'GLS';
export type PackageStatusType =
| 'in-transit'
| 'inbound-scan'
| 'delivery'
| 'transit'
| 'scanned';
Loading…
Cancel
Save