UNPKG

@ic-wallet-kit/hpl

Version:
100 lines (99 loc) 5.58 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.EditHplVirtualAccountInternalHandler = void 0; const hplWrappers_1 = require("../../../hplWrappers"); const cacheDataHandlers_1 = require("../../../internalHandlers/cacheDataHandlers"); const service_1 = require("../../../service"); const mintUtils_1 = require("../../../utils/mintUtils"); const common_1 = require("@ic-wallet-kit/common"); require("reflect-metadata"); const typedi_1 = require("typedi"); let EditHplVirtualAccountInternalHandler = class EditHplVirtualAccountInternalHandler extends common_1.BaseHandler { identifierService; canisterService; hplVirtualAccountCacheDataHandler; hplOwnerCacheDataHandler; hplMintCacheDataHandler; constructor(logger, identifierService, canisterService, hplVirtualAccountCacheDataHandler, hplOwnerCacheDataHandler, hplMintCacheDataHandler) { super(logger); this.identifierService = identifierService; this.canisterService = canisterService; this.hplVirtualAccountCacheDataHandler = hplVirtualAccountCacheDataHandler; this.hplOwnerCacheDataHandler = hplOwnerCacheDataHandler; this.hplMintCacheDataHandler = hplMintCacheDataHandler; } validate(form) { if (form.virtualAccountId === undefined) { throw new common_1.ValidationError("editing.hpl.virtual.account.internal.virtualAccountId.is.required", (0, common_1.getPropertyName)(form, f => f.virtualAccountId), "Field virtualAccountId is required"); } if (form.accountId === undefined) { throw new common_1.ValidationError("editing.hpl.virtual.account.internal.accountId.is.required", (0, common_1.getPropertyName)(form, f => f.accountId), "Field accountId is required"); } if (form.amount === undefined) { throw new common_1.ValidationError("editing.hpl.virtual.account.internal.amount.is.required", (0, common_1.getPropertyName)(form, f => f.amount), "Field amount is required"); } return Promise.resolve(); } async process(form) { const ingressActorWrapper = hplWrappers_1.IngressActorWrapper.create(this.identifierService.getAgent(), this.canisterService.getLedgerCanisterId(), this.logger); await ingressActorWrapper.updateVirtualAccount(form.virtualAccountId, form.accountId, form.amount, form.expiration); const hplData = await this.hplVirtualAccountCacheDataHandler.process({ loadType: common_1.LoadType.Cache }); const virtualAccountData = hplData.virtualAccounts.find((va) => va.virtualAccountId === form.virtualAccountId); if (!virtualAccountData) { throw new common_1.ValidationError("virtual.account.not.found", "virtualAccountId", "Virtual Account not found"); } const accessByPrincipal = virtualAccountData.virtualAccountInfo.principal; const ownerData = await this.hplOwnerCacheDataHandler.process({ principal: this.identifierService.getPrincipal(), loadType: common_1.LoadType.Cache }); let mint = { canisterId: accessByPrincipal, isMinter: false }; if ((0, mintUtils_1.getPrincipalGroupsQty)(accessByPrincipal.toString()) < 6) { mint = await this.hplMintCacheDataHandler.process({ canisterId: accessByPrincipal, loadType: common_1.LoadType.Cache }); } const result = { virtualAccountId: form.virtualAccountId, accountId: form.accountId, accessBy: accessByPrincipal.toString(), amount: form.amount, currencyAmount: "0.0", code: (0, common_1.getPxlCode)(ownerData.ownerId ? ownerData.ownerId.toString() : "", form.virtualAccountId), isMint: mint.isMinter, name: form.virtualAccountName, expiration: form.expiration, assetId: virtualAccountData.virtualAccountInfo.accountType.ft, assetSymbol: "" }; return result; } }; exports.EditHplVirtualAccountInternalHandler = EditHplVirtualAccountInternalHandler; exports.EditHplVirtualAccountInternalHandler = EditHplVirtualAccountInternalHandler = __decorate([ (0, typedi_1.Service)(), __param(0, (0, typedi_1.Inject)("ILogger")), __metadata("design:paramtypes", [Object, common_1.IdentifierService, service_1.CanisterService, cacheDataHandlers_1.HplVirtualAccountCacheDataHandler, cacheDataHandlers_1.HplOwnerCacheDataHandler, cacheDataHandlers_1.HplMintCacheDataHandler]) ], EditHplVirtualAccountInternalHandler);