/* eslint-disable @typescript-eslint/no-explicit-any */ /** @module operations */ import { ConfigParams, Context, Descriptor, IReferences, } from "pip-services4-components-node"; import { RestOperations } from "pip-services4-http-node"; import { IPurchaseOrdersClientV1, PurchaseOrderStateV1, PurchaseOrderV1, } from "bm-vslice-ecommerce-layered-client-purchaseorders-pipservices-node"; import { IPaymentsClientV1, PaymentSystemAccountV1, } from "bm-vslice-ecommerce-layered-client-payments-pipservices-node"; import { IdGenerator } from "pip-services4-data-node"; import { CredentialResolver, CredentialParams, } from "pip-services4-config-node"; export class OrdersOperationsV1 extends RestOperations { private _ordersClient: IPurchaseOrdersClientV1; private _paymentsClient: IPaymentsClientV1; private _credentialsResolver: CredentialResolver = new CredentialResolver(); private _credentials: CredentialParams; private _paymentSystem: string; private _traceId = "orders-operations"; public constructor() { super(); this._dependencyResolver.put( "purchaseorders", new Descriptor("service-purchaseorders", "client", "*", "*", "1.0") ); this._dependencyResolver.put( "payments", new Descriptor("service-payments", "client", "*", "*", "1.0") ); } public configure(config: ConfigParams): void { super.configure(config); this._credentialsResolver.configure(config); this._paymentSystem = config.getAsStringWithDefault( "payment_system", this._paymentSystem ); } public setReferences(references: IReferences): void { super.setReferences(references); this._ordersClient = this._dependencyResolver.getOneRequired( "purchaseorders" ); this._paymentsClient = this._dependencyResolver.getOneRequired("payments"); } public async getOrders(req: any, res: any): Promise { const filter = this.getFilterParams(req); try { const page = await this._ordersClient.getOrders( Context.fromTraceId(this._traceId + IdGenerator.nextShort()), filter, null ); this.sendResult(req, res, page); } catch (err) { this.sendError(req, res, err); } } public async getOrderById(req: any, res: any): Promise { const id = req.route.params.id; const customer_id = req.route.params.customer_id; try { const order = await this._ordersClient.getOrderById( Context.fromTraceId(this._traceId + IdGenerator.nextShort()), id, customer_id ); this.sendResult(req, res, order); } catch (err) { this.sendError(req, res, err); } } public async createOrder(req: any, res: any): Promise { const data = req.body; try { const order = await this._ordersClient.createOrder( Context.fromTraceId(this._traceId + IdGenerator.nextShort()), data ); this.sendResult(req, res, order); } catch (err) { this.sendError(req, res, err); } } public async updateOrder(req: any, res: any): Promise { const data = req.body; try { const order = await this._ordersClient.updateOrder( Context.fromTraceId(this._traceId + IdGenerator.nextShort()), data ); this.sendResult(req, res, order); } catch (err) { this.sendError(req, res, err); } } public async checkoutAndPay(req: any, res: any): Promise { const _order: PurchaseOrderV1 = req.body.order; const _paymentMethod = req.body.payment_method; this._credentials = await this._credentialsResolver.lookup(null); const _paymentSystemAccount = new PaymentSystemAccountV1(); _paymentSystemAccount.access_key = this._credentials.getAccessKey(); try { if (_paymentMethod.id && _paymentMethod.id.length > 0) { _order.payment_method_id = _paymentMethod.id; } else { const paymentMethod = await this._paymentsClient.createPaymentMethod( Context.fromTraceId(this._traceId + IdGenerator.nextShort()), this._paymentSystem, _paymentSystemAccount, _paymentMethod ); _order.payment_method_id = paymentMethod.id; } _order.state = PurchaseOrderStateV1.New; const order = await this._ordersClient.createOrder( Context.fromTraceId(this._traceId + IdGenerator.nextShort()), _order ); this.sendResult(req, res, order); } catch (err) { this.sendError(req, res, err); } } }