Añadir funcionalidades para eliminar personas y asignaciones en el repositorio de compañías

This commit is contained in:
David Arranz 2026-10-09 22:17:40 +02:00
parent 454e68d5c8
commit f945e9a996
13 changed files with 210 additions and 7 deletions

View File

@ -12,4 +12,10 @@ export interface ICompanyRepository {
id: UniqueID,
change: (company: Company) => Result<Company, CompanyError>
): Promise<Result<Company, CompanyError>>;
removePerson(id: UniqueID, personId: UniqueID): Promise<Result<Company, CompanyError>>;
removeAssignment(
id: UniqueID,
personId: UniqueID,
assignmentId: UniqueID
): Promise<Result<Company, CompanyError>>;
}

View File

@ -97,6 +97,14 @@ export class CompanyUseCases {
company.updatePerson(child.data, props.data)
);
}
async removePerson(companyId: string, id: string) {
const parent = this.input.id(companyId);
const child = this.input.id(id);
if (!(parent.isSuccess && child.isSuccess)) {
return Result.fail(new CompanyError("INVALID_REQUEST"));
}
return this.companies.removePerson(parent.data, child.data);
}
async addDepartment(id: string, dto: DepartmentRequestDTO) {
const parsed = this.input.id(id);
const props = this.input.department(dto);
@ -148,6 +156,15 @@ export class CompanyUseCases {
company.updateAssignment(person.data, child.data, values.data, new Date().toISOString())
);
}
async removeAssignment(companyId: string, personId: string, id: string) {
const parent = this.input.id(companyId);
const person = this.input.id(personId);
const assignment = this.input.id(id);
if (!(parent.isSuccess && person.isSuccess && assignment.isSuccess)) {
return Result.fail(new CompanyError("INVALID_REQUEST"));
}
return this.companies.removeAssignment(parent.data, person.data, assignment.data);
}
async addSite(id: string, dto: SiteRequestDTO) {
const parsed = this.input.id(id);
const props = this.input.site(dto);

View File

@ -4,9 +4,11 @@ export type CompanyErrorCode =
| "CONTACT_NOT_FOUND"
| "SITE_NOT_FOUND"
| "PERSON_NOT_FOUND"
| "PERSON_IN_USE"
| "DEPARTMENT_NOT_FOUND"
| "DEPARTMENT_NAME_EXISTS"
| "ASSIGNMENT_NOT_FOUND"
| "ASSIGNMENT_IN_USE"
| "ASSIGNMENT_CONFLICT"
| "INACTIVE_ASSIGNMENT_TARGET";
export class CompanyError extends Error {

View File

@ -109,6 +109,20 @@ export class Company extends AggregateRoot<CompanyProps> {
)
);
}
removePerson(id: UniqueID): Result<Company, CompanyError> {
if (!this.people.some((person) => person.id.equals(id))) {
return Result.fail(new CompanyError("PERSON_NOT_FOUND"));
}
if (this.assignments.some((assignment) => assignment.personId.equals(id))) {
return Result.fail(new CompanyError("PERSON_IN_USE"));
}
return Result.ok(
new Company(
{ ...this.props, people: this.people.filter((person) => !person.id.equals(id)) },
this.id
)
);
}
private departmentNameExists(name: string, except?: UniqueID): boolean {
const key = name.normalize("NFC").toLowerCase();
return this.departments.some(
@ -256,6 +270,20 @@ export class Company extends AggregateRoot<CompanyProps> {
)
);
}
removeAssignment(personId: UniqueID, id: UniqueID): Result<Company, CompanyError> {
const assignment = this.assignments.find(
(item) => item.id.equals(id) && item.personId.equals(personId)
);
if (!assignment) {
return Result.fail(new CompanyError("ASSIGNMENT_NOT_FOUND"));
}
return Result.ok(
new Company(
{ ...this.props, assignments: this.assignments.filter((item) => !item.id.equals(id)) },
this.id
)
);
}
addSite(site: Site): Result<Company, CompanyError> {
if (!site.companyId.equals(this.id)) {
return Result.fail(new CompanyError("INVALID_REQUEST"));

View File

@ -126,6 +126,17 @@ export function registerCompanyRoutes(
post("/:companyId/people/:id/update", PersonRequestSchema, ChildParamsSchema, (dto, params) =>
useCases.updatePerson(params.companyId, params.id, dto)
);
router.post("/:companyId/people/:id/delete", (request, response, next) => {
const params = ChildParamsSchema.safeParse(request.params);
if (!(params.success && z.strictObject({}).safeParse(request.body).success)) {
sendError(response, "INVALID_REQUEST", 400);
return;
}
useCases
.removePerson(params.data.companyId, params.data.id)
.then((result) => sendResult(response, result))
.catch(next);
});
post(
"/:companyId/departments",
DepartmentRequestSchema,
@ -146,6 +157,17 @@ export function registerCompanyRoutes(
(dto, params) => useCases.addAssignment(params.companyId, params.personId, dto),
201
);
router.post("/:companyId/people/:personId/assignments/:id/delete", (request, response, next) => {
const params = AssignmentParamsSchema.safeParse(request.params);
if (!(params.success && z.strictObject({}).safeParse(request.body).success)) {
sendError(response, "INVALID_REQUEST", 400);
return;
}
useCases
.removeAssignment(params.data.companyId, params.data.personId, params.data.id)
.then((result) => sendResult(response, result))
.catch(next);
});
post(
"/:companyId/people/:personId/assignments/:id/update",
AssignmentUpdateRequestSchema,

View File

@ -102,6 +102,55 @@ export class SequelizeCompanyRepository implements ICompanyRepository {
await CompanyModel.create(this.mapper.toPersistence(company).company);
return company;
}
async removePerson(
id: UniqueID,
personId: UniqueID
): Promise<DomainResult<Company, CompanyError>> {
return this.database.transaction(async (transaction) => {
const current = (await this.load(id, transaction, true)).getOrUndefined();
if (!current) {
return Result.fail(new CompanyError("COMPANY_NOT_FOUND"));
}
const changed = current.removePerson(personId);
if (!changed.isSuccess) {
return changed;
}
try {
await PersonModel.destroy({
where: { id: personId.toPrimitive(), companyId: id.toPrimitive() },
transaction,
});
} catch {
return Result.fail(new CompanyError("PERSON_IN_USE"));
}
return changed;
});
}
async removeAssignment(
id: UniqueID,
personId: UniqueID,
assignmentId: UniqueID
): Promise<DomainResult<Company, CompanyError>> {
return this.database.transaction(async (transaction) => {
const current = (await this.load(id, transaction, true)).getOrUndefined();
if (!current) {
return Result.fail(new CompanyError("COMPANY_NOT_FOUND"));
}
const changed = current.removeAssignment(personId, assignmentId);
if (!changed.isSuccess) {
return changed;
}
await AssignmentRevisionModel.destroy({
where: { assignmentId: assignmentId.toPrimitive() },
transaction,
});
await AssignmentModel.destroy({
where: { id: assignmentId.toPrimitive(), companyId: id.toPrimitive() },
transaction,
});
return changed;
});
}
async update(
id: UniqueID,
change: (company: Company) => DomainResult<Company, CompanyError>

View File

@ -909,7 +909,15 @@
"removed": "The logo will be removed on save.",
"remove": "Remove logo",
"undo": "Undo logo change"
}
},
"deletePerson": "Delete person",
"deletingPerson": "Deleting…",
"deletePersonConfirm": "Delete {{name}}? It can only be deleted when it has no registered relationships.",
"deletePersonError": "This person cannot be deleted because it has registered relationships.",
"deleteAssignment": "Delete assignment",
"deletingAssignment": "Deleting…",
"deleteAssignmentConfirm": "Delete the assignment to {{name}}?",
"deleteAssignmentError": "The assignment could not be deleted."
},
"access": {
"helpTitle": "What is it and what is it for?",

View File

@ -909,7 +909,15 @@
"removed": "Se quitará el logotipo al guardar.",
"remove": "Quitar logotipo",
"undo": "Deshacer cambio de logotipo"
}
},
"deletePerson": "Eliminar persona",
"deletingPerson": "Eliminando…",
"deletePersonConfirm": "¿Eliminar a {{name}}? Solo se puede eliminar si no tiene relaciones registradas.",
"deletePersonError": "No se puede eliminar esta persona porque tiene relaciones registradas.",
"deleteAssignment": "Eliminar asignación",
"deletingAssignment": "Eliminando…",
"deleteAssignmentConfirm": "¿Eliminar la asignación al departamento {{name}}?",
"deleteAssignmentError": "No se puede eliminar la asignación."
},
"access": {
"helpTitle": "¿Qué es y para qué sirve?",

View File

@ -1 +1 @@
export { saveAssignment } from "./save-assignment.api";
export { deleteAssignment, saveAssignment } from "./save-assignment.api";

View File

@ -1,6 +1,14 @@
import { CompanyResponseSchema } from "../../../../common";
import { companyRequest } from "../../../companies/shared/api/company-request";
export const deleteAssignment = (companyId: string, personId: string, id: string, token: string) =>
companyRequest(
`/${companyId}/people/${personId}/assignments/${id}/delete`,
CompanyResponseSchema,
{},
token
);
export const saveAssignment = (
companyId: string,
personId: string,

View File

@ -11,6 +11,8 @@ import { useTranslation } from "react-i18next";
import type { CompanyDTO, PersonDTO } from "../../../../common";
import { AssignmentEditorDialog, AssignmentHistoryDialog } from "../../../assignments";
import { deleteAssignment } from "../../../assignments/shared/api";
import { deletePerson } from "../../shared/api";
export function PersonDetails({
company,
@ -28,6 +30,8 @@ export function PersonDetails({
const { t, i18n } = useTranslation("companies");
const [editor, setEditor] = useState<string | undefined>();
const [historyId, setHistoryId] = useState<string | undefined>();
const [deleting, setDeleting] = useState(false);
const [deletingAssignment, setDeletingAssignment] = useState<string | undefined>();
const assignments = company.assignments.filter((item) => item.personId === person.id);
const selected = assignments.find((item) => item.id === editor);
const history = assignments.find((item) => item.id === historyId);
@ -46,9 +50,31 @@ export function PersonDetails({
{person.isContact ? ` · ${t("isContact")}` : ""}
</p>
</div>
<Button onClick={onEdit} variant="outline">
{t("editPerson")}
</Button>
<div className="flex flex-wrap gap-2">
<Button onClick={onEdit} variant="outline">
{t("editPerson")}
</Button>
<Button
disabled={deleting}
onClick={async () => {
if (!window.confirm(t("deletePersonConfirm", { name: person.name }))) {
return;
}
setDeleting(true);
try {
await deletePerson(company.id, person.id, csrfToken);
onBack();
} catch {
window.alert(t("deletePersonError"));
} finally {
setDeleting(false);
}
}}
variant="destructive"
>
{t(deleting ? "deletingPerson" : "deletePerson")}
</Button>
</div>
</div>
<Card>
<CardContent className="grid gap-4 pt-6 sm:grid-cols-3">
@ -132,6 +158,32 @@ export function PersonDetails({
<EntityAction onClick={() => setHistoryId(item.id)}>
{t("history")}
</EntityAction>
<EntityAction
onClick={async () => {
if (
!window.confirm(
t("deleteAssignmentConfirm", {
name: departmentName(item.departmentId),
})
)
) {
return;
}
setDeletingAssignment(item.id);
try {
await deleteAssignment(company.id, person.id, item.id, csrfToken);
window.location.reload();
} catch {
window.alert(t("deleteAssignmentError"));
} finally {
setDeletingAssignment(undefined);
}
}}
>
{t(
deletingAssignment === item.id ? "deletingAssignment" : "deleteAssignment"
)}
</EntityAction>
</EntityRowActions>
),
},

View File

@ -1 +1 @@
export { savePerson } from "./save-person.api";
export { deletePerson, savePerson } from "./save-person.api";

View File

@ -13,3 +13,6 @@ export const savePerson = (
body,
token
);
export const deletePerson = (companyId: string, id: string, token: string) =>
companyRequest(`/${companyId}/people/${id}/delete`, CompanyResponseSchema, {}, token);