import { Order } from '@cupcake/domain' ; import { EmailNotification } from '../EmailNotification' ; import { CreateOrderPayload } from './CreateOrderPayload' ; import { InvalidOrderPayloadError } from './InvalidOrderPayloadError' ; import { OrderRepository } from './OrderRepository' ; import { validateOrder } from './validateOrder' ; import nanoid = require ( 'nanoid' ) ; export class OrderService { constructor ( private readonly emailNotification , private readonly orderRepository ) { } public async create ( payload ) { const validationResult = validateOrder ( payload ) ; if ( validationResult !== true ) { throw new InvalidOrderPayloadError ( validationResult ) ; } const id = nanoid ( ) ; const order = { ... payload , id , } ; await this . orderRepository . save ( order ) ; await this . emailNotification . sendOrderNotification ( order ) ; return order ; } public async load ( id ) { return this . orderRepository . load ( id ) ; } public async all ( ) { return this . orderRepository . all ( ) ; } }