import 'reflect-metadata'; import { BaseController } from '@inmotion/api'; import { IApplicationContext, InmotionContext, InmotionRequest, InmotionResponse } from '@inmotion/core'; import { ErrorService } from '@inmotion/utils'; import { API_TOKEN, ProductRepositoryFactory } from '@whittaker/swift-libs'; import { CartCreationService, CartCreationServiceFactory } from '../services/cart-creation.service'; import { CartBuilderInput, CartState } from '../services/models.cart'; export class CartController extends BaseController { protected appContext: IApplicationContext; protected _cartCreationService: CartCreationService; constructor(appContext: IApplicationContext) { super(); appContext.Token = API_TOKEN; // appContext.HttpProvider = new IHttpProvider(); this.appContext = appContext; this._cartCreationService = new CartCreationServiceFactory( appContext, new ProductRepositoryFactory(appContext).build() ).build(); } public setContext(host: string, token: string, context: InmotionContext): void { super.setContext(host, API_TOKEN, context); } public async build(request: InmotionRequest): Promise> { let response = new InmotionResponse(true, null, []); try { const input: CartBuilderInput = request.params; response.data = await this._cartCreationService.updateOrCreateCartAndBuildState(input); response.success = true; } catch (e) { response.success = false; response.errors.push(ErrorService.getMessage(e, 'Error in method: CartController.build')); } return response; } public async getCart(request: InmotionRequest): Promise> { let response = new InmotionResponse(true, null, []); try { response.data = await this._cartCreationService.getOrCreateCartAndBuildState(request.params.cartGuid); response.success = true; } catch (e) { response.success = false; response.errors.push(ErrorService.getMessage(e, 'Error in method: CartController.getCart')); } return response; } }