UNPKG

@vendure/core

Version:

A modern, headless ecommerce framework

85 lines 4.34 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.CustomerChannelAssignmentService = void 0; const common_1 = require("@nestjs/common"); const shared_constants_1 = require("@vendure/common/lib/shared-constants"); const config_service_1 = require("../../../config/config.service"); const customer_entity_1 = require("../../../entity/customer/customer.entity"); const channel_service_1 = require("../../services/channel.service"); const customer_service_1 = require("../../services/customer.service"); /** * @description * Handles the assignment of a signed-in Customer to the active Channel. * * @docsCategory services */ let CustomerChannelAssignmentService = class CustomerChannelAssignmentService { constructor(configService, customerService, channelService) { this.configService = configService; this.customerService = customerService; this.channelService = channelService; } /** * @description * Assigns the active Customer to the active Channel where appropriate. Does not block the * request: a Customer the strategy declines to assign may still operate on the Channel for the * current session, just without a persisted membership. */ async tryAssignToActiveChannel(ctx) { const userId = ctx.activeUserId; if (!userId) { return; } const { disableAuth, customerChannelAssignmentStrategy } = this.configService.authOptions; // The default Channel and disableAuth dev mode always assign and never consult the strategy. const isGated = !disableAuth && ctx.channel.code !== shared_constants_1.DEFAULT_CHANNEL_CODE; if (isGated) { const member = await this.customerService.findOneByUserId(ctx, userId, true); if (member) { return; } } // No Customer record (e.g. an Administrator) means there is nothing to assign. const customer = await this.customerService.findOneByUserId(ctx, userId, false); if (!customer) { return; } const canAssign = !isGated || (await customerChannelAssignmentStrategy.canAssignCustomerToChannel(ctx, customer, ctx.channelId)); if (canAssign) { await this.assignToActiveChannel(ctx, customer.id); } } async assignToActiveChannel(ctx, customerId) { try { await this.channelService.assignToChannels(ctx, customer_entity_1.Customer, customerId, [ctx.channelId]); } catch (e) { // Two requests for the same Customer can reach this at once and both try to add the same // Channel. If the database rejects ours as a duplicate, the other one already did the // work, so let it pass. Any other failure is real and should surface. // See https://github.com/vendurehq/vendure/issues/834 const isDuplicateError = e.code === 'ER_DUP_ENTRY' /* MySQL/MariaDB */ || e.code === '23505'; /* Postgres */ if (!isDuplicateError) { throw e; } } } }; exports.CustomerChannelAssignmentService = CustomerChannelAssignmentService; exports.CustomerChannelAssignmentService = CustomerChannelAssignmentService = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [config_service_1.ConfigService, customer_service_1.CustomerService, channel_service_1.ChannelService]) ], CustomerChannelAssignmentService); //# sourceMappingURL=customer-channel-assignment.service.js.map