72 lines
2.3 KiB
TypeScript
72 lines
2.3 KiB
TypeScript
import { ClientCompanyNotificationOverrideAdapterMongoose } from "./ClientCompanyNotificationOverride.Adapter.Mongoose";
|
|
import {
|
|
IClientCompanyNotificationOverride,
|
|
IClientCompanyNotificationOverrideAdapter,
|
|
} from "./ClientCompanyNotificationOverride.Interface";
|
|
import ClientsList from "../Clients/Clients";
|
|
import { validatePermissionsByCompany, validateSessionUser } from "../../helpers/check";
|
|
|
|
type FindClientCompanyOverrideParams = {
|
|
clientId: string;
|
|
companyId: string;
|
|
sessionUser: string;
|
|
};
|
|
|
|
type UpsertClientCompanyOverrideParams = FindClientCompanyOverrideParams & {
|
|
data: Partial<
|
|
Omit<
|
|
IClientCompanyNotificationOverride,
|
|
"id" | "clientId" | "companyId" | "createdAt" | "updatedAt"
|
|
>
|
|
>;
|
|
};
|
|
|
|
class ClientCompanyNotificationOverrideManager {
|
|
overrides: IClientCompanyNotificationOverrideAdapter;
|
|
|
|
constructor() {
|
|
this.overrides = new ClientCompanyNotificationOverrideAdapterMongoose();
|
|
}
|
|
|
|
public async findOne(
|
|
params: FindClientCompanyOverrideParams
|
|
): Promise<IClientCompanyNotificationOverride | null> {
|
|
await this.validateAccess(params);
|
|
|
|
return this.overrides.findOne({
|
|
clientId: params.clientId,
|
|
companyId: params.companyId,
|
|
});
|
|
}
|
|
|
|
public async upsert(
|
|
params: UpsertClientCompanyOverrideParams
|
|
): Promise<IClientCompanyNotificationOverride> {
|
|
await this.validateAccess(params);
|
|
|
|
return this.overrides.upsert(params.clientId, params.companyId, params.data);
|
|
}
|
|
|
|
private async validateAccess(params: FindClientCompanyOverrideParams): Promise<void> {
|
|
await validateSessionUser({ sessionUser: params.sessionUser });
|
|
await validatePermissionsByCompany({
|
|
companyId: params.companyId,
|
|
sessionUser: params.sessionUser,
|
|
});
|
|
|
|
const client = await ClientsList.clients.findOne({ _id: params.clientId });
|
|
|
|
if (!client) {
|
|
throw new Error("El cliente no existe");
|
|
}
|
|
|
|
if (String(client.companyId) !== String(params.companyId)) {
|
|
throw new Error("El cliente no pertenece a la organización");
|
|
}
|
|
}
|
|
}
|
|
|
|
const ClientCompanyNotificationOverride = new ClientCompanyNotificationOverrideManager();
|
|
|
|
export default ClientCompanyNotificationOverride;
|