refactor: replace client deletion with user detachment and update client schema to support optional user association
This commit is contained in:
@@ -6,6 +6,7 @@ import {
|
||||
CreateClientByUserParams,
|
||||
PaginateClientsParams,
|
||||
PaginateClientsResults,
|
||||
DetachClientUserParams,
|
||||
} from "./Clients.Interface";
|
||||
import { Document, FilterQuery, Model, Schema, model } from "mongoose";
|
||||
|
||||
@@ -96,4 +97,8 @@ export class ClientsAdapterMongoose implements IClientsAdapter {
|
||||
pages: Math.ceil(count / filters.limit),
|
||||
};
|
||||
}
|
||||
|
||||
public async detachUser(data: DetachClientUserParams): Promise<void> {
|
||||
await this.clientList.updateOne({ _id: data.clientId }, { $unset: { userId: "" } }).exec();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,10 @@ export type FindClientsParams = {
|
||||
status?: boolean;
|
||||
};
|
||||
|
||||
export type DetachClientUserParams = {
|
||||
clientId: string;
|
||||
};
|
||||
|
||||
export type FindClientByIdParams = {
|
||||
id: string;
|
||||
companyId: string;
|
||||
@@ -77,7 +81,7 @@ export type DeleteClientParams = {
|
||||
export interface IClient {
|
||||
id?: string;
|
||||
companyId: string;
|
||||
userId: string;
|
||||
userId?: string;
|
||||
fromClientId: string;
|
||||
status: boolean;
|
||||
firstName: string;
|
||||
@@ -116,6 +120,7 @@ export interface IClientsAdapter {
|
||||
find(filters: FindClientsParams): Promise<IClient[]>;
|
||||
findOne(filters: FindClientsParams): Promise<IClientDocument | null>;
|
||||
paginate(filters: PaginateClientsParams): Promise<PaginateClientsResults>;
|
||||
detachUser(data: DetachClientUserParams): Promise<void>;
|
||||
}
|
||||
|
||||
export interface IClientsManager {
|
||||
|
||||
@@ -195,7 +195,9 @@ class ClientManager implements IClientsManager {
|
||||
|
||||
if (appointment) {
|
||||
appointment.clientId = data.toClientId;
|
||||
appointment.userId = clientTo.userId;
|
||||
if (clientTo.userId) {
|
||||
appointment.userId = clientTo.userId;
|
||||
}
|
||||
await appointment.save();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -284,7 +284,7 @@ class UsersManager implements IUsersManager {
|
||||
userId: data.sessionUser,
|
||||
});
|
||||
|
||||
//Eliminar los clientes creados con el usuario.
|
||||
//Desvincular los clientes creados con el usuario sin borrar el historial de la organizacion.
|
||||
const clients = await ClientsList.clients.find({
|
||||
userId: data.sessionUser,
|
||||
});
|
||||
@@ -294,21 +294,7 @@ class UsersManager implements IUsersManager {
|
||||
continue;
|
||||
}
|
||||
|
||||
//Tengo que pasarlo como sesionUSer al duenio de la organizacion. para que pueda eliminar.
|
||||
const orgClient = await CompaniesList.companies.findOne({
|
||||
_id: String(client.companyId),
|
||||
});
|
||||
|
||||
if (!orgClient) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//En este caso borra el cliente pero no chequea el saldo de la cuenta.
|
||||
await ClientsList.deleteClient({
|
||||
clientId: String(client.id),
|
||||
checkCashOnAccount: false,
|
||||
sessionUser: String(orgClient.ownerId),
|
||||
});
|
||||
await ClientsList.clients.detachUser({ clientId: String(client.id) });
|
||||
}
|
||||
|
||||
//Eliminar las conversaciones
|
||||
@@ -1279,14 +1265,7 @@ class UsersManager implements IUsersManager {
|
||||
for (const client of clients) {
|
||||
if (!client.id) continue;
|
||||
|
||||
const orgClient = await CompaniesList.companies.findOne({ _id: String(client.companyId) });
|
||||
if (!orgClient) continue;
|
||||
|
||||
await ClientsList.deleteClient({
|
||||
clientId: String(client.id),
|
||||
checkCashOnAccount: false,
|
||||
sessionUser: String(orgClient.ownerId),
|
||||
});
|
||||
await ClientsList.clients.detachUser({ clientId: String(client.id) });
|
||||
}
|
||||
|
||||
await MessageConversationList.deleteConversationsByUser({ userId: data.userId });
|
||||
|
||||
@@ -2,7 +2,7 @@ import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
export default function getAvatar(
|
||||
userId: string,
|
||||
userId: string | null | undefined,
|
||||
avatar: string | null | undefined,
|
||||
fallback: string
|
||||
): string {
|
||||
|
||||
Reference in New Issue
Block a user