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,
|
CreateClientByUserParams,
|
||||||
PaginateClientsParams,
|
PaginateClientsParams,
|
||||||
PaginateClientsResults,
|
PaginateClientsResults,
|
||||||
|
DetachClientUserParams,
|
||||||
} from "./Clients.Interface";
|
} from "./Clients.Interface";
|
||||||
import { Document, FilterQuery, Model, Schema, model } from "mongoose";
|
import { Document, FilterQuery, Model, Schema, model } from "mongoose";
|
||||||
|
|
||||||
@@ -96,4 +97,8 @@ export class ClientsAdapterMongoose implements IClientsAdapter {
|
|||||||
pages: Math.ceil(count / filters.limit),
|
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;
|
status?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type DetachClientUserParams = {
|
||||||
|
clientId: string;
|
||||||
|
};
|
||||||
|
|
||||||
export type FindClientByIdParams = {
|
export type FindClientByIdParams = {
|
||||||
id: string;
|
id: string;
|
||||||
companyId: string;
|
companyId: string;
|
||||||
@@ -77,7 +81,7 @@ export type DeleteClientParams = {
|
|||||||
export interface IClient {
|
export interface IClient {
|
||||||
id?: string;
|
id?: string;
|
||||||
companyId: string;
|
companyId: string;
|
||||||
userId: string;
|
userId?: string;
|
||||||
fromClientId: string;
|
fromClientId: string;
|
||||||
status: boolean;
|
status: boolean;
|
||||||
firstName: string;
|
firstName: string;
|
||||||
@@ -116,6 +120,7 @@ export interface IClientsAdapter {
|
|||||||
find(filters: FindClientsParams): Promise<IClient[]>;
|
find(filters: FindClientsParams): Promise<IClient[]>;
|
||||||
findOne(filters: FindClientsParams): Promise<IClientDocument | null>;
|
findOne(filters: FindClientsParams): Promise<IClientDocument | null>;
|
||||||
paginate(filters: PaginateClientsParams): Promise<PaginateClientsResults>;
|
paginate(filters: PaginateClientsParams): Promise<PaginateClientsResults>;
|
||||||
|
detachUser(data: DetachClientUserParams): Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface IClientsManager {
|
export interface IClientsManager {
|
||||||
|
|||||||
@@ -195,7 +195,9 @@ class ClientManager implements IClientsManager {
|
|||||||
|
|
||||||
if (appointment) {
|
if (appointment) {
|
||||||
appointment.clientId = data.toClientId;
|
appointment.clientId = data.toClientId;
|
||||||
|
if (clientTo.userId) {
|
||||||
appointment.userId = clientTo.userId;
|
appointment.userId = clientTo.userId;
|
||||||
|
}
|
||||||
await appointment.save();
|
await appointment.save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -284,7 +284,7 @@ class UsersManager implements IUsersManager {
|
|||||||
userId: data.sessionUser,
|
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({
|
const clients = await ClientsList.clients.find({
|
||||||
userId: data.sessionUser,
|
userId: data.sessionUser,
|
||||||
});
|
});
|
||||||
@@ -294,21 +294,7 @@ class UsersManager implements IUsersManager {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//Tengo que pasarlo como sesionUSer al duenio de la organizacion. para que pueda eliminar.
|
await ClientsList.clients.detachUser({ clientId: String(client.id) });
|
||||||
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),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Eliminar las conversaciones
|
//Eliminar las conversaciones
|
||||||
@@ -1279,14 +1265,7 @@ class UsersManager implements IUsersManager {
|
|||||||
for (const client of clients) {
|
for (const client of clients) {
|
||||||
if (!client.id) continue;
|
if (!client.id) continue;
|
||||||
|
|
||||||
const orgClient = await CompaniesList.companies.findOne({ _id: String(client.companyId) });
|
await ClientsList.clients.detachUser({ clientId: String(client.id) });
|
||||||
if (!orgClient) continue;
|
|
||||||
|
|
||||||
await ClientsList.deleteClient({
|
|
||||||
clientId: String(client.id),
|
|
||||||
checkCashOnAccount: false,
|
|
||||||
sessionUser: String(orgClient.ownerId),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await MessageConversationList.deleteConversationsByUser({ userId: data.userId });
|
await MessageConversationList.deleteConversationsByUser({ userId: data.userId });
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import fs from "fs";
|
|||||||
import path from "path";
|
import path from "path";
|
||||||
|
|
||||||
export default function getAvatar(
|
export default function getAvatar(
|
||||||
userId: string,
|
userId: string | null | undefined,
|
||||||
avatar: string | null | undefined,
|
avatar: string | null | undefined,
|
||||||
fallback: string
|
fallback: string
|
||||||
): string {
|
): string {
|
||||||
|
|||||||
Reference in New Issue
Block a user