174 lines
7.8 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.statsSchema = exports.subUserSchema = exports.emailPreferencesSchema = exports.userSchema = exports.whmcsClientSchema = exports.userAuthSchema = exports.addressFormSchema = exports.addressSchema = void 0;
exports.addressFormToRequest = addressFormToRequest;
exports.combineToUser = combineToUser;
const zod_1 = require("zod");
const schema_1 = require("../common/schema");
const stringOrNull = zod_1.z.union([zod_1.z.string(), zod_1.z.null()]);
const booleanLike = zod_1.z.union([zod_1.z.boolean(), zod_1.z.number(), zod_1.z.string()]);
const numberLike = zod_1.z.union([zod_1.z.number(), zod_1.z.string()]);
const normalizeBoolean = (value) => {
if (value === undefined)
return undefined;
if (value === null)
return null;
if (typeof value === "boolean")
return value;
if (typeof value === "number")
return value === 1;
if (typeof value === "string") {
const normalized = value.trim().toLowerCase();
return normalized === "1" || normalized === "true" || normalized === "yes" || normalized === "on";
}
return null;
};
exports.addressSchema = zod_1.z.object({
address1: stringOrNull.optional(),
address2: stringOrNull.optional(),
city: stringOrNull.optional(),
state: stringOrNull.optional(),
postcode: stringOrNull.optional(),
country: stringOrNull.optional(),
countryCode: stringOrNull.optional(),
phoneNumber: stringOrNull.optional(),
phoneCountryCode: stringOrNull.optional(),
});
exports.addressFormSchema = zod_1.z.object({
address1: zod_1.z.string().min(1, "Address line 1 is required").max(200, "Address line 1 is too long").trim(),
address2: zod_1.z.string().max(200, "Address line 2 is too long").trim().optional(),
city: zod_1.z.string().min(1, "City is required").max(100, "City name is too long").trim(),
state: zod_1.z.string().min(1, "State/Prefecture is required").max(100, "State/Prefecture name is too long").trim(),
postcode: zod_1.z.string().min(1, "Postcode is required").max(20, "Postcode is too long").trim(),
country: zod_1.z.string().min(1, "Country is required").max(100, "Country name is too long").trim(),
countryCode: schema_1.countryCodeSchema.optional(),
phoneNumber: zod_1.z.string().optional(),
phoneCountryCode: zod_1.z.string().optional(),
});
exports.userAuthSchema = zod_1.z.object({
id: zod_1.z.string().uuid(),
email: zod_1.z.string().email(),
role: zod_1.z.enum(["USER", "ADMIN"]),
emailVerified: zod_1.z.boolean(),
mfaEnabled: zod_1.z.boolean(),
lastLoginAt: zod_1.z.string().optional(),
createdAt: zod_1.z.string(),
updatedAt: zod_1.z.string(),
});
const emailPreferencesSchema = zod_1.z.object({
general: booleanLike.optional(),
invoice: booleanLike.optional(),
support: booleanLike.optional(),
product: booleanLike.optional(),
domain: booleanLike.optional(),
affiliate: booleanLike.optional(),
}).transform(prefs => ({
general: normalizeBoolean(prefs.general),
invoice: normalizeBoolean(prefs.invoice),
support: normalizeBoolean(prefs.support),
product: normalizeBoolean(prefs.product),
domain: normalizeBoolean(prefs.domain),
affiliate: normalizeBoolean(prefs.affiliate),
}));
exports.emailPreferencesSchema = emailPreferencesSchema;
const subUserSchema = zod_1.z.object({
id: numberLike,
name: zod_1.z.string(),
email: zod_1.z.string(),
is_owner: booleanLike.optional(),
}).transform(user => ({
id: Number(user.id),
name: user.name,
email: user.email,
is_owner: normalizeBoolean(user.is_owner),
}));
exports.subUserSchema = subUserSchema;
const statsSchema = zod_1.z.record(zod_1.z.string(), zod_1.z.union([zod_1.z.string(), zod_1.z.number(), zod_1.z.boolean()])).optional();
exports.statsSchema = statsSchema;
exports.whmcsClientSchema = zod_1.z.object({
id: numberLike,
email: zod_1.z.string(),
firstname: zod_1.z.string().nullable().optional(),
lastname: zod_1.z.string().nullable().optional(),
fullname: zod_1.z.string().nullable().optional(),
companyname: zod_1.z.string().nullable().optional(),
phonenumber: zod_1.z.string().nullable().optional(),
phonenumberformatted: zod_1.z.string().nullable().optional(),
telephoneNumber: zod_1.z.string().nullable().optional(),
status: zod_1.z.string().nullable().optional(),
language: zod_1.z.string().nullable().optional(),
defaultgateway: zod_1.z.string().nullable().optional(),
defaultpaymethodid: numberLike.nullable().optional(),
currency: numberLike.nullable().optional(),
currency_code: zod_1.z.string().nullable().optional(),
tax_id: zod_1.z.string().nullable().optional(),
allowSingleSignOn: booleanLike.nullable().optional(),
email_verified: booleanLike.nullable().optional(),
marketing_emails_opt_in: booleanLike.nullable().optional(),
notes: zod_1.z.string().nullable().optional(),
datecreated: zod_1.z.string().nullable().optional(),
lastlogin: zod_1.z.string().nullable().optional(),
address: exports.addressSchema.nullable().optional(),
email_preferences: emailPreferencesSchema.nullable().optional(),
customfields: zod_1.z.record(zod_1.z.string(), zod_1.z.string()).optional(),
users: zod_1.z.array(subUserSchema).optional(),
stats: statsSchema.optional(),
}).transform(data => ({
...data,
id: typeof data.id === 'number' ? data.id : Number(data.id),
allowSingleSignOn: normalizeBoolean(data.allowSingleSignOn),
email_verified: normalizeBoolean(data.email_verified),
marketing_emails_opt_in: normalizeBoolean(data.marketing_emails_opt_in),
defaultpaymethodid: data.defaultpaymethodid ? Number(data.defaultpaymethodid) : null,
currency: data.currency ? Number(data.currency) : null,
}));
exports.userSchema = exports.userAuthSchema.extend({
firstname: zod_1.z.string().nullable().optional(),
lastname: zod_1.z.string().nullable().optional(),
fullname: zod_1.z.string().nullable().optional(),
companyname: zod_1.z.string().nullable().optional(),
phonenumber: zod_1.z.string().nullable().optional(),
language: zod_1.z.string().nullable().optional(),
currencyCode: zod_1.z.string().nullable().optional(),
address: exports.addressSchema.optional(),
});
function addressFormToRequest(form) {
const emptyToNull = (value) => {
if (value === undefined)
return undefined;
const trimmed = value?.trim();
return trimmed ? trimmed : null;
};
return exports.addressSchema.parse({
address1: emptyToNull(form.address1),
address2: emptyToNull(form.address2 ?? null),
city: emptyToNull(form.city),
state: emptyToNull(form.state),
postcode: emptyToNull(form.postcode),
country: emptyToNull(form.country),
countryCode: emptyToNull(form.countryCode ?? null),
phoneNumber: emptyToNull(form.phoneNumber ?? null),
phoneCountryCode: emptyToNull(form.phoneCountryCode ?? null),
});
}
function combineToUser(userAuth, whmcsClient) {
return exports.userSchema.parse({
id: userAuth.id,
email: userAuth.email,
role: userAuth.role,
emailVerified: userAuth.emailVerified,
mfaEnabled: userAuth.mfaEnabled,
lastLoginAt: userAuth.lastLoginAt,
createdAt: userAuth.createdAt,
updatedAt: userAuth.updatedAt,
firstname: whmcsClient.firstname || null,
lastname: whmcsClient.lastname || null,
fullname: whmcsClient.fullname || null,
companyname: whmcsClient.companyname || null,
phonenumber: whmcsClient.phonenumberformatted || whmcsClient.phonenumber || whmcsClient.telephoneNumber || null,
language: whmcsClient.language || null,
currencyCode: whmcsClient.currency_code || null,
address: whmcsClient.address || undefined,
});
}
//# sourceMappingURL=schema.js.map