@vulog/aima-user
Version:
User management module for the AIMA platform. This module provides comprehensive functionality to manage users, profiles, billing groups, and user-related operations.
595 lines (573 loc) • 19.2 kB
JavaScript
// src/acceptTAndC.ts
import { z } from "zod";
var schema = z.object({
userId: z.string().uuid(),
cityId: z.string().uuid()
});
var acceptTAndC = async (client, userId, cityId) => {
const result = schema.safeParse({ userId, cityId });
if (!result.success) {
throw new TypeError("Invalid args", {
cause: result.error.issues
});
}
await client.post(
`boapi/proxy/user/fleets/${client.clientOptions.fleetId}/cities/${cityId}/users/${userId}/agreements`
);
};
// src/assignBillingGroup.ts
import { z as z2 } from "zod";
var schema2 = z2.object({
entityId: z2.string().trim().min(1).uuid(),
billingGroupId: z2.string().trim().min(1).uuid()
});
var assignBillingGroup = async (client, entityId, billingGroupId) => {
const result = schema2.safeParse({ entityId, billingGroupId });
if (!result.success) {
throw new TypeError("Invalid args", {
cause: result.error.issues
});
}
await client.post(
`/boapi/proxy/user/fleets/${client.clientOptions.fleetId}/billingGroup/${result.data.billingGroupId}/entities/${result.data.entityId}`
);
};
// src/createBusinessProfile.ts
import { z as z3 } from "zod";
var createBusinessProfileSchema = z3.object({
userId: z3.string().nonempty().uuid(),
businessId: z3.string().nonempty().uuid(),
data: z3.object({
emailConsent: z3.boolean(),
email: z3.string().email(),
requestId: z3.string().nonempty().uuid(),
costCenterId: z3.string().uuid().nullish()
})
});
var createBusinessProfile = async (client, userId, businessId, data) => {
const result = createBusinessProfileSchema.safeParse({
userId,
businessId,
data
});
if (!result.success) {
throw new TypeError("Invalid argd", {
cause: result.error.issues
});
}
return client.post(
`/boapi/proxy/business/fleets/${client.clientOptions.fleetId}/business/${businessId}/user/${userId}`,
result.data
).then(({ data: p }) => p);
};
// src/createUser.ts
import { z as z4 } from "zod";
var createUserSchema = z4.object({
userName: z4.string().min(1),
password: z4.string().min(1),
locale: z4.string().length(5),
email: z4.string().min(1).email(),
dataPrivacyConsent: z4.boolean().optional(),
profilingConsent: z4.boolean().optional(),
marketingConsent: z4.boolean().optional(),
phoneNumber: z4.string().optional()
});
var createUserOptionsSchema = z4.object({
tcApproval: z4.boolean().default(true),
skipUserNotification: z4.boolean().default(false)
}).default({
tcApproval: true,
skipUserNotification: false
});
var createUser = async (client, user, option) => {
const resultUser = createUserSchema.safeParse(user);
if (!resultUser.success) {
throw new TypeError("Invalid user", {
cause: resultUser.error.issues
});
}
const resultOptions = createUserOptionsSchema.safeParse(option);
if (!resultOptions.success) {
throw new TypeError("Invalid options", {
cause: resultOptions.error.issues
});
}
const searchParams = new URLSearchParams();
searchParams.append("tcApproval", resultOptions.data.tcApproval.toString());
searchParams.append("skipUserNotification", resultOptions.data.skipUserNotification.toString());
return client.post(
`/boapi/proxy/user/fleets/${client.clientOptions.fleetId}/userDefault?${searchParams.toString()}`,
resultUser.data
).then(({ data }) => data);
};
// src/findUser.ts
import { z as z6 } from "zod";
// src/types.ts
import { z as z5 } from "zod";
var accountStatus = ["INCOMPLETE", "PENDING", "APPROVED", "REJECTED", "SUSPENDED", "ARCHIVED"];
var personalInformationUserTypes = [
"IDENTITY",
"USERNAME",
"BIRTH",
"NATIONALITY",
"NOTES",
"GENDER",
"PERSONAL_COMPANY",
"FISCAL",
"ADDRESS",
"MEMBERSHIP"
];
var personalInformationUserPaths = [
"/identity/firstName",
"/identity/lastName",
"/identity/middleName",
"/identity/preferredName",
"/birth/birthDate",
"/birth/countryBirth",
"/birth/provinceBirth",
"/birth/cityBirth",
"/nationality",
"/notes",
"/gender",
"/personalCompany/companyName",
"/personalCompany/companyVat",
"/personalCompany/companyAddress/streetName",
"/personalCompany/companyAddress/city",
"/personalCompany/companyAddress/postalCode",
"/personalCompany/companyAddress/region",
"/personalCompany/companyAddress/country",
"/personalCompany/companyAddress/number",
"/personalCompany/companyAddress/addressAdditionalInformation",
"/fiscalInformation/fiscal",
"/fiscalInformation/personalVatNumber",
"/fiscalInformation/sdi",
"/fiscalInformation/pecAddress",
"/fiscalInformation/marketReference",
"/address/streetName",
"/address/city",
"/address/postalCode",
"/address/region",
"/address/country",
"/address/number",
"/address/addressAdditionalInformation",
"/membership"
];
var personalInformationUserTypeSchema = z5.enum(personalInformationUserTypes);
var personalInformationProfileTypes = ["ID_NUMBER", "PHONE_NUMBER", "EMAIL", "BILLING_ADDRESS"];
var personalInformationProfileTypeSchema = z5.enum(personalInformationProfileTypes);
// src/findUser.ts
var searchTypes = ["email", "username", "phoneNumber"];
var querySchema = z6.object({
searchType: z6.enum(searchTypes),
searchQuery: z6.string().min(1),
types: z6.array(personalInformationUserTypeSchema).min(1)
});
var findUser = async (client, searchType, searchQuery, types) => {
const result = querySchema.safeParse({
searchType,
searchQuery,
types
});
if (!result.success) {
throw new TypeError("Invalid arguments", {
cause: result.error.issues
});
}
return client.post(
`/boapi/proxy/user/fleets/${client.clientOptions.fleetId}/users/pi/find?types=${result.data.types.join(",")}`,
{
[result.data.searchType]: result.data.searchQuery
}
).then(({ data }) => data);
};
// src/getProfilePersonalInfoById.ts
import { z as z7 } from "zod";
var argsSchema = z7.object({
userId: z7.string().trim().min(1).uuid(),
profileId: z7.string().trim().min(1).uuid(),
types: z7.array(personalInformationProfileTypeSchema).min(1)
});
var getProfilePersonalInfoById = async (client, userId, profileId, types) => {
const result = argsSchema.safeParse({ userId, profileId, types });
if (!result.success) {
throw new TypeError("Invalid args", {
cause: result.error.issues
});
}
return client.get(
`/boapi/proxy/user/fleets/${client.clientOptions.fleetId}/users/${result.data.userId}/profiles/${result.data.profileId}/pi?types=${result.data.types.join(",")}`
).then(({ data }) => data);
};
// src/getRegistrationOverview.ts
import { z as z8 } from "zod";
var getRegistrationOverview = async (client, userId) => {
const result = z8.string().uuid().safeParse(userId);
if (!result.success) {
throw new TypeError("Invalid userId", {
cause: result.error.issues
});
}
return client.get(`boapi/proxy/user/fleets/${client.clientOptions.fleetId}/users/${userId}/services`).then(({ data: { userId: uid, ...data } }) => data);
};
// src/getUserById.ts
import { z as z9 } from "zod";
var getUserById = async (client, id, addAccountStatus = false) => {
const result = z9.string().trim().min(1).uuid().safeParse(id);
if (!result.success) {
throw new TypeError("Invalid id", {
cause: result.error.issues
});
}
const user = await client.get(`/boapi/proxy/user/fleets/${client.clientOptions.fleetId}/users/${id}/status`).then(({ data }) => data);
if (addAccountStatus) {
user.accountStatus = await client.get(`/boapi/proxy/user/fleets/${client.clientOptions.fleetId}/users/${id}`).then(({ data: { accountStatus: accountStatus2 } }) => accountStatus2);
}
return user;
};
// src/getUserPersonalInfoById.ts
import { z as z10 } from "zod";
var argsSchema2 = z10.object({
id: z10.string().trim().min(1).uuid(),
types: z10.array(personalInformationUserTypeSchema).min(1)
});
var getUserPersonalInfoById = async (client, id, types) => {
const result = argsSchema2.safeParse({ id, types });
if (!result.success) {
throw new TypeError("Invalid args", {
cause: result.error.issues
});
}
return client.get(
`/boapi/proxy/user/fleets/${client.clientOptions.fleetId}/users/${result.data.id}/pi?types=${result.data.types.join(",")}`
).then(({ data }) => data);
};
// src/getUsersPIByIds.ts
import { z as z11 } from "zod";
var argsSchema3 = z11.object({
ids: z11.array(z11.string().trim().min(1).uuid()).min(1),
types: z11.array(personalInformationUserTypeSchema).min(1)
});
var getUsersPIByIds = async (client, ids, types) => {
const result = argsSchema3.safeParse({ ids, types });
if (!result.success) {
throw new TypeError("Invalid args", {
cause: result.error.issues
});
}
return client.post(
`/boapi/proxy/user/fleets/${client.clientOptions.fleetId}/users/pi/list?${result.data.types.map((type) => `types=${type}`).join("&")}`,
{
userIds: result.data.ids
}
).then(({ data }) => data.content);
};
// src/label.ts
import { z as z12 } from "zod";
var schema3 = z12.object({
userId: z12.string().uuid()
});
var schemaData = z12.object({
userId: z12.string().uuid(),
labelId: z12.number().positive()
});
var getLabelsForUser = async (client, userId) => {
const result = schema3.safeParse({ userId });
if (!result.success) {
throw new TypeError("Invalid args", {
cause: result.error.issues
});
}
return client.get(`boapi/proxy/user/fleets/${client.clientOptions.fleetId}/users/${userId}/userLabels`).then(({ data }) => data);
};
var addLabelForUser = async (client, userId, labelId) => {
const result = schemaData.safeParse({ userId, labelId });
if (!result.success) {
throw new TypeError("Invalid args", {
cause: result.error.issues
});
}
await client.post(`boapi/proxy/user/fleets/${client.clientOptions.fleetId}/users/${userId}/userLabels`, {
labelId
});
};
var removeLabelForUser = async (client, userId, labelId) => {
const result = schemaData.safeParse({ userId, labelId });
if (!result.success) {
throw new TypeError("Invalid args", {
cause: result.error.issues
});
}
await client.delete(
`boapi/proxy/user/fleets/${client.clientOptions.fleetId}/users/${userId}/userLabels/${labelId}`
);
};
// src/setServicesStatus.ts
import { z as z13 } from "zod";
var registrationStatus = ["APPROVED", "INCOMPLETE", "PENDING", "REJECTED", "SUSPENDED", "UNREGISTERED"];
var schema4 = z13.object({
disableEmailNotification: z13.boolean(),
operatorProfileId: z13.string().uuid(),
actions: z13.array(
z13.object({
reasonForChange: z13.string(),
serviceId: z13.string().uuid(),
status: z13.enum(registrationStatus)
})
).min(1)
});
var setServicesStatus = async (client, profileId, servicesUpdate) => {
const resultProfileId = z13.string().uuid().safeParse(profileId);
if (!resultProfileId.success) {
throw new TypeError("Invalid profileId", {
cause: resultProfileId.error.issues
});
}
const resultServices = schema4.safeParse(servicesUpdate);
if (!resultServices.success) {
throw new TypeError("Invalid servicesUpdate", {
cause: resultServices.error.issues
});
}
await client.post(
`boapi/proxy/user/fleets/${client.clientOptions.fleetId}/profiles/${profileId}/serviceRegistrations`,
servicesUpdate
);
};
// src/registerUserToService.ts
import { z as z14 } from "zod";
var registerUserToService = async (client, entityId, serviceId) => {
const resultEntityId = z14.string().uuid().safeParse(entityId);
if (!resultEntityId.success) {
throw new TypeError("Invalid entityId", {
cause: resultEntityId.error.issues
});
}
const resultServiceId = z14.string().uuid().safeParse(serviceId);
if (!resultServiceId.success) {
throw new TypeError("Invalid serviceId", {
cause: resultServiceId.error.issues
});
}
await client.put(
`boapi/proxy/user/fleets/${client.clientOptions.fleetId}/entities/${entityId}/services/${serviceId}`
);
};
// src/updateProfilePersonalInfo.ts
import { z as z15 } from "zod";
var paths = ["/phoneNumber", "/email", "/idNumber"];
var schema5 = z15.object({
userId: z15.string().trim().min(1).uuid(),
profileId: z15.string().trim().min(1).uuid(),
actions: z15.array(
z15.union([
z15.object({
op: z15.enum(["add", "replace"]),
path: z15.enum(paths),
value: z15.string().min(1)
}),
z15.object({
op: z15.literal("remove"),
path: z15.enum(paths)
})
])
).min(1)
});
var updateProfilePersonalInfo = async (client, userId, profileId, actions) => {
const result = schema5.safeParse({ userId, profileId, actions });
if (!result.success) {
throw new TypeError("Invalid args", {
cause: result.error.issues
});
}
await client.patch(
`/boapi/proxy/user/fleets/${client.clientOptions.fleetId}/users/${userId}/profiles/${profileId}/pi`,
actions,
{
headers: {
"Content-Type": "application/json-patch+json"
}
}
);
};
// src/updateUser.ts
import { z as z16 } from "zod";
var schema6 = z16.object({
id: z16.string().trim().min(1).uuid(),
user: z16.object({
locale: z16.string(),
accountStatus: z16.enum(accountStatus),
dataPrivacyConsent: z16.boolean(),
marketingConsent: z16.boolean(),
surveyConsent: z16.boolean(),
shareDataConsent: z16.boolean(),
profilingConsent: z16.boolean(),
membershipNumber: z16.string()
}).partial()
});
var updateUser = async (client, id, user) => {
const result = schema6.safeParse({ id, user });
if (!result.success) {
throw new TypeError("Invalid args", {
cause: result.error.issues
});
}
return client.post(`/boapi/proxy/user/fleets/${client.clientOptions.fleetId}/users/${id}`, user).then(({ data }) => data);
};
// src/updateUserPersonalInfo.ts
import { z as z17 } from "zod";
var schema7 = z17.object({
userId: z17.string().trim().min(1).uuid(),
actions: z17.array(
z17.union([
z17.object({
op: z17.enum(["add", "replace"]),
path: z17.enum(personalInformationUserPaths),
value: z17.string().min(1)
}),
z17.object({
op: z17.literal("remove"),
path: z17.enum(personalInformationUserPaths)
})
])
).min(1)
});
var updateUserPersonalInfo = async (client, userId, actions) => {
const result = schema7.safeParse({ userId, actions });
if (!result.success) {
throw new TypeError("Invalid args", {
cause: result.error.issues
});
}
await client.patch(`/boapi/proxy/user/fleets/${client.clientOptions.fleetId}/users/${userId}/pi`, actions, {
headers: {
"Content-Type": "application/json-patch+json"
}
}).then(({ data }) => data);
};
// src/getEntity.ts
import { z as z18 } from "zod";
var getEntity = async (client, entityId) => {
const result = z18.string().trim().min(1).uuid().safeParse(entityId);
if (!result.success) {
throw new TypeError("Invalid entity id", {
cause: result.error.issues
});
}
const entity = await client.get(`/boapi/proxy/user/fleets/${client.clientOptions.fleetId}/entities/${entityId}`).then(({ data }) => data);
return entity;
};
// src/getFleetBillingGroups.ts
import { createPaginableOptionsSchema } from "@vulog/aima-core";
var getFleetBillingGroups = async (client, options) => {
const PaginableOptionsSchema = createPaginableOptionsSchema().default({});
const resultOptions = PaginableOptionsSchema.safeParse(options);
if (!resultOptions.success) {
throw new TypeError("Invalid options", {
cause: resultOptions.error.issues
});
}
const finalOptions = resultOptions.data;
const searchParams = new URLSearchParams();
searchParams.append("page", finalOptions.page.toString());
searchParams.append("size", finalOptions.pageSize.toString());
return client.get(
`/boapi/proxy/user/fleets/${client.clientOptions.fleetId}/billingGroup?${searchParams.toString()}`
).then(({ data, headers }) => {
return {
data,
page: headers.number,
pageSize: headers.size,
total: headers.totalelements,
totalPages: headers.totalpages
};
}).catch((error) => {
throw new Error(`Failed to get fleet billing groups: ${error.message}`);
});
};
// src/getUsersByIds.ts
import { z as z19 } from "zod";
var argsSchema4 = z19.object({
ids: z19.array(z19.string().trim().min(1).uuid()).min(1)
});
var getUsersByIds = async (client, ids) => {
const result = argsSchema4.safeParse({ ids });
if (!result.success) {
throw new TypeError("Invalid args", {
cause: result.error.issues
});
}
return client.post(`/boapi/proxy/user/fleets/${client.clientOptions.fleetId}/users/list`, result.data.ids).then(({ data }) => data);
};
// src/getUsers.ts
import { createPaginableOptionsSchema as createPaginableOptionsSchema2 } from "@vulog/aima-core";
import { z as z20 } from "zod";
var statusSchema = z20.enum(["PENDING", "INCOMPLETE", "SUSPENDED", "REJECTED", "APPROVED"]);
var profileTypeSchema = z20.enum(["Single", "Business"]);
var userFiltersSchema = z20.object({
status: statusSchema.optional(),
profileType: profileTypeSchema.optional(),
serviceId: z20.string().uuid().optional()
});
var sortSchema = z20.enum(["registrationDate", "userName", "firstName", "lastName", "updateDate"]);
var getUsers = async (client, options) => {
const paginableOptionsSchema = createPaginableOptionsSchema2(
userFiltersSchema.default({}),
sortSchema.optional().default("registrationDate")
).default({});
const resultOptions = paginableOptionsSchema.safeParse(options);
if (!resultOptions.success) {
throw new TypeError("Invalid options", {
cause: resultOptions.error.issues
});
}
const finalOptions = resultOptions.data;
const searchParams = new URLSearchParams();
searchParams.append("page", finalOptions.page.toString());
searchParams.append("size", finalOptions.pageSize.toString());
searchParams.append("sort", `${finalOptions.sort.toString()},${finalOptions.sortDirection.toString()}`);
Object.entries(finalOptions.filters).forEach(([key, value]) => {
if (value === void 0) {
return;
}
searchParams.append(key, value);
});
return client.get(`/boapi/proxy/user/fleets/${client.clientOptions.fleetId}/users?${searchParams.toString()}`).then(({ data, headers }) => {
return {
data,
page: headers.number,
pageSize: headers.size,
total: headers.totalelements,
totalPages: headers.totalpages
};
});
};
export {
acceptTAndC,
accountStatus,
addLabelForUser,
assignBillingGroup,
createBusinessProfile,
createUser,
findUser,
getEntity,
getFleetBillingGroups,
getLabelsForUser,
getProfilePersonalInfoById,
getRegistrationOverview,
getUserById,
getUserPersonalInfoById,
getUsers,
getUsersByIds,
getUsersPIByIds,
personalInformationProfileTypeSchema,
personalInformationProfileTypes,
personalInformationUserPaths,
personalInformationUserTypeSchema,
personalInformationUserTypes,
registerUserToService,
removeLabelForUser,
setServicesStatus,
updateProfilePersonalInfo,
updateUser,
updateUserPersonalInfo
};