Skip to content

Instantly share code, notes, and snippets.

@coderbyheart
Last active March 7, 2019 11:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coderbyheart/5137f9b900d68ba268757e9cbcc9bdac to your computer and use it in GitHub Desktop.
Save coderbyheart/5137f9b900d68ba268757e9cbcc9bdac to your computer and use it in GitHub Desktop.
functional repository
import { SIMVendor } from './SIMVendor';
export interface SIMVendorRepository {
findAll(): Promise<SIMVendor[]>;
/**
* @throws EntityNotFoundError if the SIMVendor cannot be found.
*/
getByUUID(uuid: string): Promise<SIMVendor>;
/**
* @throws EntityNotFoundError if the SIMVendor cannot be found.
*/
getByICCID(iccid: string): Promise<SIMVendor>;
}
import { SIMVendorRepository } from './SIMVendorRepository';
import { EntityNotFoundError } from '@nrfcloud/api-core';
import { SIMVendor } from './SIMVendor';
export class StaticSIMVendorRepository implements SIMVendorRepository {
async findAll(): Promise<SIMVendor[]> {
return [
SIMVendor({
slug: 'acme',
name: 'ACME Telco',
url: 'https://example.com/',
iccidRegExp: /^890199[0-9]{12}[0-9]F$/,
$meta: {
uuid: 'd4908caf-dc39-4d82-9606-b2f64504776a',
createdAt: new Date('2018-11-28T11:35:30.807Z'),
},
}),
];
}
/**
* @throws EntityNotFoundError if the SIM cannot be found.
*/
async getByUUID(uuid: string): Promise<SIMVendor> {
const vendor = (await this.findAll()).find(
({ $meta: { uuid: u } }) => uuid === u,
);
if (!vendor) {
throw new EntityNotFoundError(`Vendor "${uuid}" not found!`);
}
return vendor;
}
/**
* @throws EntityNotFoundError if the SIM cannot be found.
*/
async getByICCID(iccid: string): Promise<SIMVendor> {
const vendor = (await this.findAll()).find(({ iccidRegExp }) =>
iccidRegExp.test(iccid),
);
if (!vendor) {
throw new EntityNotFoundError(`Vendor for "${iccid}" not found!`);
}
return vendor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment