143 lines
6.6 KiB
JavaScript
143 lines
6.6 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.listCustomers = listCustomers;
|
|
exports.getCustomer = getCustomer;
|
|
exports.createCustomer = createCustomer;
|
|
exports.updateCustomer = updateCustomer;
|
|
exports.flagCustomer = flagCustomer;
|
|
exports.unflagCustomer = unflagCustomer;
|
|
exports.validateCustomerLicense = validateCustomerLicense;
|
|
exports.uploadLicenseImage = uploadLicenseImage;
|
|
exports.getLicenseImageFile = getLicenseImageFile;
|
|
exports.approveLicense = approveLicense;
|
|
const licenseValidationService_1 = require("../../services/licenseValidationService");
|
|
const storage_1 = require("../../lib/storage");
|
|
const errors_1 = require("../../http/errors");
|
|
const customer_presenter_1 = require("./customer.presenter");
|
|
const repo = __importStar(require("./customer.repo"));
|
|
async function listCustomers(companyId, query) {
|
|
const page = query.page ?? 1;
|
|
const pageSize = query.pageSize ?? 20;
|
|
const { q, flagged } = query;
|
|
const safeQ = q ? q.trim().slice(0, 100) : undefined;
|
|
const where = { companyId };
|
|
if (flagged !== undefined)
|
|
where.flagged = flagged === 'true';
|
|
if (safeQ) {
|
|
where.OR = [
|
|
{ firstName: { contains: safeQ, mode: 'insensitive' } },
|
|
{ lastName: { contains: safeQ, mode: 'insensitive' } },
|
|
{ email: { contains: safeQ, mode: 'insensitive' } },
|
|
];
|
|
}
|
|
const [customers, total] = await repo.findMany(where, (page - 1) * pageSize, pageSize);
|
|
return (0, customer_presenter_1.presentCustomerList)(customers.map(customer_presenter_1.presentCustomer), { total, page, pageSize, totalPages: Math.ceil(total / pageSize) });
|
|
}
|
|
async function getCustomer(id, companyId) {
|
|
const customer = await repo.findById(id, companyId);
|
|
if (!customer)
|
|
throw new errors_1.NotFoundError('Customer not found');
|
|
return (0, customer_presenter_1.presentCustomer)(customer);
|
|
}
|
|
async function createCustomer(data, companyId) {
|
|
const customer = await repo.create({
|
|
...data,
|
|
companyId,
|
|
dateOfBirth: data.dateOfBirth ? new Date(data.dateOfBirth) : null,
|
|
licenseExpiry: data.licenseExpiry ? new Date(data.licenseExpiry) : null,
|
|
licenseIssuedAt: data.licenseIssuedAt ? new Date(data.licenseIssuedAt) : null,
|
|
});
|
|
if (data.licenseExpiry) {
|
|
await (0, licenseValidationService_1.validateAndFlagLicense)(customer.id, companyId).catch(() => null);
|
|
}
|
|
return (0, customer_presenter_1.presentCustomer)(customer);
|
|
}
|
|
async function updateCustomer(id, companyId, data) {
|
|
const result = await repo.updateMany(id, companyId, {
|
|
...data,
|
|
dateOfBirth: data.dateOfBirth ? new Date(data.dateOfBirth) : undefined,
|
|
licenseExpiry: data.licenseExpiry ? new Date(data.licenseExpiry) : undefined,
|
|
licenseIssuedAt: data.licenseIssuedAt ? new Date(data.licenseIssuedAt) : undefined,
|
|
});
|
|
if (result.count === 0)
|
|
throw new errors_1.NotFoundError('Customer not found');
|
|
if (data.licenseExpiry)
|
|
await (0, licenseValidationService_1.validateAndFlagLicense)(id, companyId).catch(() => null);
|
|
return (0, customer_presenter_1.presentCustomer)(await repo.findUniqueOrThrow(id, companyId));
|
|
}
|
|
async function flagCustomer(id, companyId, reason) {
|
|
await repo.updateMany(id, companyId, { flagged: true, flagReason: reason ?? null });
|
|
}
|
|
async function unflagCustomer(id, companyId) {
|
|
await repo.updateMany(id, companyId, { flagged: false, flagReason: null });
|
|
}
|
|
async function validateCustomerLicense(id, companyId) {
|
|
const customer = await repo.findByIdSimple(id, companyId);
|
|
if (!customer)
|
|
throw new errors_1.NotFoundError('Customer not found');
|
|
return (0, licenseValidationService_1.validateAndFlagLicense)(customer.id, companyId);
|
|
}
|
|
async function uploadLicenseImage(id, companyId, file) {
|
|
const customer = await repo.findByIdSimple(id, companyId);
|
|
if (!customer)
|
|
throw new errors_1.NotFoundError('Customer not found');
|
|
const url = await (0, storage_1.uploadImage)(file.buffer, `companies/${companyId}/customers/${customer.id}`);
|
|
if (customer.licenseImageUrl) {
|
|
await (0, storage_1.deleteImage)(customer.licenseImageUrl).catch(() => null);
|
|
}
|
|
return (0, customer_presenter_1.presentCustomer)(await repo.updateById(customer.id, { licenseImageUrl: url }));
|
|
}
|
|
async function getLicenseImageFile(id, companyId) {
|
|
const customer = await repo.findByIdSimple(id, companyId);
|
|
if (!customer || !customer.licenseImageUrl)
|
|
throw new errors_1.NotFoundError('License image not found');
|
|
const filePath = (0, storage_1.resolveStoredFilePath)(customer.licenseImageUrl);
|
|
if (!filePath)
|
|
throw new errors_1.NotFoundError('License image not found');
|
|
return filePath;
|
|
}
|
|
async function approveLicense(id, companyId, decision, note, approverName) {
|
|
const customer = await repo.findByIdSimple(id, companyId);
|
|
if (!customer)
|
|
throw new errors_1.NotFoundError('Customer not found');
|
|
return (0, customer_presenter_1.presentCustomer)(await repo.updateById(customer.id, {
|
|
licenseValidationStatus: decision === 'APPROVE' ? 'APPROVED' : 'DENIED',
|
|
licenseApprovedBy: approverName,
|
|
licenseApprovedAt: new Date(),
|
|
licenseApprovalNote: note ?? null,
|
|
}));
|
|
}
|
|
//# sourceMappingURL=customer.service.js.map
|