Last active
March 7, 2019 11:55
-
-
Save coderbyheart/5137f9b900d68ba268757e9cbcc9bdac to your computer and use it in GitHub Desktop.
functional repository
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
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>; | |
} |
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
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