175 lines
8.0 KiB
JavaScript
175 lines
8.0 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.getCompany = getCompany;
|
|
exports.updateCompany = updateCompany;
|
|
exports.getBrand = getBrand;
|
|
exports.updateBrand = updateBrand;
|
|
exports.uploadLogo = uploadLogo;
|
|
exports.uploadHeroImage = uploadHeroImage;
|
|
exports.checkSubdomainAvailability = checkSubdomainAvailability;
|
|
exports.setCustomDomain = setCustomDomain;
|
|
exports.getCustomDomainStatus = getCustomDomainStatus;
|
|
exports.removeCustomDomain = removeCustomDomain;
|
|
exports.getContractSettings = getContractSettings;
|
|
exports.updateContractSettings = updateContractSettings;
|
|
exports.getInsurancePolicies = getInsurancePolicies;
|
|
exports.createInsurancePolicy = createInsurancePolicy;
|
|
exports.updateInsurancePolicy = updateInsurancePolicy;
|
|
exports.deleteInsurancePolicy = deleteInsurancePolicy;
|
|
exports.getPricingRules = getPricingRules;
|
|
exports.createPricingRule = createPricingRule;
|
|
exports.updatePricingRule = updatePricingRule;
|
|
exports.deletePricingRule = deletePricingRule;
|
|
exports.getAccountingSettings = getAccountingSettings;
|
|
exports.updateAccountingSettings = updateAccountingSettings;
|
|
exports.getApiKey = getApiKey;
|
|
exports.regenerateApiKey = regenerateApiKey;
|
|
const storage_1 = require("../../lib/storage");
|
|
const errors_1 = require("../../http/errors");
|
|
const company_presenter_1 = require("./company.presenter");
|
|
const repo = __importStar(require("./company.repo"));
|
|
function buildPaymentMethodsEnabled(input) {
|
|
const methods = [];
|
|
if (input.amanpayMerchantId && input.amanpaySecretKey)
|
|
methods.push('AMANPAY');
|
|
if (input.paypalEmail)
|
|
methods.push('PAYPAL');
|
|
return methods;
|
|
}
|
|
async function getCompany(companyId) {
|
|
return (0, company_presenter_1.presentCompany)(await repo.findCompany(companyId));
|
|
}
|
|
async function updateCompany(companyId, data) {
|
|
return (0, company_presenter_1.presentCompany)(await repo.updateCompany(companyId, data));
|
|
}
|
|
async function getBrand(companyId) {
|
|
return (0, company_presenter_1.presentBrand)(await repo.findBrand(companyId));
|
|
}
|
|
async function updateBrand(companyId, body, companyName, companySlug) {
|
|
const current = await repo.findBrand(companyId);
|
|
const paymentMethodsEnabled = buildPaymentMethodsEnabled({
|
|
amanpayMerchantId: body.amanpayMerchantId ?? current?.amanpayMerchantId,
|
|
amanpaySecretKey: body.amanpaySecretKey ?? current?.amanpaySecretKey,
|
|
paypalEmail: body.paypalEmail ?? current?.paypalEmail,
|
|
});
|
|
return (0, company_presenter_1.presentBrand)(await repo.upsertBrand(companyId, { ...body, paymentMethodsEnabled }, { displayName: body.displayName ?? companyName, subdomain: companySlug, paymentMethodsEnabled, ...body }));
|
|
}
|
|
async function uploadLogo(companyId, companyName, companySlug, file) {
|
|
const url = await (0, storage_1.uploadImage)(file, `companies/${companyId}/brand`, 'logo');
|
|
return (0, company_presenter_1.presentBrand)(await repo.upsertBrand(companyId, { logoUrl: url }, { displayName: companyName, subdomain: companySlug, logoUrl: url }));
|
|
}
|
|
async function uploadHeroImage(companyId, companyName, companySlug, file) {
|
|
const url = await (0, storage_1.uploadImage)(file, `companies/${companyId}/brand`, 'hero');
|
|
return (0, company_presenter_1.presentBrand)(await repo.upsertBrand(companyId, { heroImageUrl: url }, { displayName: companyName, subdomain: companySlug, heroImageUrl: url }));
|
|
}
|
|
async function checkSubdomainAvailability(subdomain, companyId) {
|
|
const existing = await repo.findBrandBySubdomain(subdomain, companyId);
|
|
return { available: !existing };
|
|
}
|
|
async function setCustomDomain(companyId, companyName, companySlug, customDomain) {
|
|
const normalized = customDomain.toLowerCase().trim();
|
|
const existing = await repo.findBrandByCustomDomain(normalized, companyId);
|
|
if (existing)
|
|
throw new errors_1.ConflictError('This custom domain is already in use');
|
|
return repo.upsertBrand(companyId, { customDomain: normalized, customDomainVerified: false, customDomainAddedAt: new Date() }, { displayName: companyName, subdomain: companySlug, customDomain: normalized, customDomainVerified: false, customDomainAddedAt: new Date() });
|
|
}
|
|
async function getCustomDomainStatus(companyId) {
|
|
const brand = await repo.findBrand(companyId);
|
|
const customDomain = brand?.customDomain ?? null;
|
|
return {
|
|
customDomain,
|
|
verified: brand?.customDomainVerified ?? false,
|
|
status: customDomain ? (brand?.customDomainVerified ? 'verified' : 'pending_dns') : 'not_configured',
|
|
dnsTarget: process.env.NEXT_PUBLIC_CUSTOM_DOMAIN_TARGET ?? 'cname.vercel-dns.com',
|
|
};
|
|
}
|
|
async function removeCustomDomain(companyId) {
|
|
const result = await repo.clearCustomDomain(companyId);
|
|
return { success: result.count > 0 };
|
|
}
|
|
async function getContractSettings(companyId) {
|
|
return repo.findContractSettings(companyId);
|
|
}
|
|
async function updateContractSettings(companyId, data) {
|
|
return repo.upsertContractSettings(companyId, data);
|
|
}
|
|
async function getInsurancePolicies(companyId) {
|
|
return repo.findInsurancePolicies(companyId);
|
|
}
|
|
async function createInsurancePolicy(companyId, data) {
|
|
return repo.createInsurancePolicy(companyId, data);
|
|
}
|
|
async function updateInsurancePolicy(id, companyId, data) {
|
|
const result = await repo.updateInsurancePolicy(id, companyId, data);
|
|
if (result.count === 0)
|
|
throw new errors_1.NotFoundError('Insurance policy not found');
|
|
return repo.findInsurancePolicyOrThrow(id);
|
|
}
|
|
async function deleteInsurancePolicy(id, companyId) {
|
|
const result = await repo.deleteInsurancePolicy(id, companyId);
|
|
if (result.count === 0)
|
|
throw new errors_1.NotFoundError('Insurance policy not found');
|
|
}
|
|
async function getPricingRules(companyId) {
|
|
return repo.findPricingRules(companyId);
|
|
}
|
|
async function createPricingRule(companyId, data) {
|
|
return repo.createPricingRule(companyId, data);
|
|
}
|
|
async function updatePricingRule(id, companyId, data) {
|
|
const result = await repo.updatePricingRule(id, companyId, data);
|
|
if (result.count === 0)
|
|
throw new errors_1.NotFoundError('Pricing rule not found');
|
|
return repo.findPricingRuleOrThrow(id);
|
|
}
|
|
async function deletePricingRule(id, companyId) {
|
|
const result = await repo.deletePricingRule(id, companyId);
|
|
if (result.count === 0)
|
|
throw new errors_1.NotFoundError('Pricing rule not found');
|
|
}
|
|
async function getAccountingSettings(companyId) {
|
|
return repo.findAccountingSettings(companyId);
|
|
}
|
|
async function updateAccountingSettings(companyId, data) {
|
|
return repo.upsertAccountingSettings(companyId, data);
|
|
}
|
|
async function getApiKey(companyId) {
|
|
return repo.findApiKey(companyId);
|
|
}
|
|
async function regenerateApiKey(companyId) {
|
|
return repo.regenerateApiKey(companyId);
|
|
}
|
|
//# sourceMappingURL=company.service.js.map
|