I'm facing a perplexing logic problem that is eluding my understanding. Within the context of using next-connect
, I have a function designed to update an entry in the database:
.put(async (req, res) => {
const data = req.body;
const { dob, roles, cases } = data ?? {};
const convertedDob = dob ? new Date(dob) : null;
const roleIds = roles?.map((role: Role) => {
return { id: role.id };
});
const caseIds = cases?.map((_case: Case) => {
return { id: _case.id };
});
data.dob = convertedDob;
delete data.roles;
delete data.formData;
const user = await getUserDataFromSession(req, res);
throwIfNoCurrentTeam(user?.currentTeam);
try {
const person: Person = await prisma.person.update({
where: { id: data.id },
data: {
...data,
roles: { connect: roleIds },
cases: { connect: caseIds },
},
});
if (person && person.linkedIds) {
// get linkedId arrays of all people linked to this person
const associatedPeopleIdArrays = await prisma.$transaction(
person.linkedIds.map((id) =>
prisma.person.findUnique({
where: { id },
select: { id: true, linkedIds: true },
}),
),
);
const backLinkArray = associatedPeopleIdArrays.map((linkedPerson) => {
const linkedPersonIds = linkedPerson?.linkedIds;
const linkedPersonOwnId = linkedPerson?.id;
// if the array of linkedIds already includes the person we are back-linking, enter next check
if (linkedPersonIds?.includes(person.id)) {
// if they both include each other, do nothing
if (person.linkedIds.includes(linkedPersonOwnId as string)) {
return { id: linkedPersonOwnId, linkedIds: linkedPersonIds };
}
// if linkedPersonIds includes person.id but person.linkedIds does not include linkedPersonOwnId, remove the relationship
return { id: linkedPersonOwnId, linkedIds: linkedPersonIds.filter((id) => id !== person.id) };
// else add the current person's id to each person's array that we are back-linking
} else {
return {
id: linkedPersonOwnId,
linkedIds: linkedPersonIds ? [...linkedPersonIds, person.id] : [person.id],
};
}
});
// write the new linkedId array to each associated person
await prisma.$transaction(
backLinkArray.map((linkedPerson) =>
prisma.person.update({
where: { id: linkedPerson?.id },
data: {
linkedIds: linkedPerson?.linkedIds,
},
}),
),
);
}
return res.status(200).json({ updated: person });
} catch (err: any) {
console.log({ err });
handleApiError('Failed to update person', 500, err);
}
});
In attempting to unravel this puzzle, I have included comments throughout the code to aid comprehension. While logs have been strategically placed to shed light on the process, the challenge lies within the intricate if/else logic.
The function effectively establishes relationships between individuals. When adding an id
to one person's linkedIds
array, the same id
should be added to the associated person's array as well.
However, there exists a need for additional criteria to sever a relationship when an id
is removed from a person's linkedIds
. This aspect seems to be faltering. The issue arises after entering the statement
if (linkedPersonIds?.includes(person.id))
, followed by invariably executing the subsequent condition if (person.linkedIds.includes(linkedPersonOwnId as string))
.
It was anticipated that deleting an id
from the linkedIds
array would yield a false outcome, yet it persistently returns true, impeding the deletion of the relationship. What am I overlooking? Where has my understanding lapsed?
Pardon the chaotic presentation, and I trust you can navigate through this labyrinthine scenario!