UNPKG

@area37/vendure-plugin-customer-approve

Version:

Vendure plugin for approving customers

66 lines (65 loc) 2.92 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); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CustomerApproveService = void 0; const common_1 = require("@nestjs/common"); const core_1 = require("@vendure/core"); const generated_types_1 = require("@vendure/common/lib/generated-types"); let CustomerApproveService = class CustomerApproveService { connection; userService; customerService; historyService; eventBus; constructor(connection, userService, customerService, historyService, eventBus) { this.connection = connection; this.userService = userService; this.customerService = customerService; this.historyService = historyService; this.eventBus = eventBus; } async switchApproveCustomer(ctx, customerId) { const customer = await this.customerService.findOne(ctx, customerId, [ "user", ]); if (!customer) throw new Error("Customer not found"); const { user } = customer; if (user instanceof core_1.User) { user.verified = !user?.verified; await this.connection .getRepository(ctx, core_1.User) .save(user, { reload: false }); if (user.verified) { await this.historyService.createHistoryEntryForCustomer({ customerId: customer.id, ctx, type: generated_types_1.HistoryEntryType.CUSTOMER_VERIFIED, data: { strategy: core_1.NATIVE_AUTH_STRATEGY_NAME, }, }); await this.eventBus.publish(new core_1.AccountVerifiedEvent(ctx, customer)); } return !user?.verified; } return false; } }; exports.CustomerApproveService = CustomerApproveService; exports.CustomerApproveService = CustomerApproveService = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [core_1.TransactionalConnection, core_1.UserService, core_1.CustomerService, core_1.HistoryService, core_1.EventBus]) ], CustomerApproveService);