feat: fix and add several things

This commit is contained in:
2026-04-28 01:48:11 +00:00
parent c3d55b898f
commit db727e0a9d
17 changed files with 541 additions and 135 deletions
+37 -1
View File
@@ -175,7 +175,7 @@ export const opportunities = {
const record = await prisma.opportunity.findFirst({
where: isNumeric
? ({ cwOpportunityId: Number(identifier) } as any)
? { id: Number(identifier) }
: { uid: identifier as string },
include: {
company: { include: { contacts: true, companyAddresses: true } },
@@ -548,4 +548,40 @@ export const opportunities = {
})
);
},
/**
* Delete Opportunity
*
* Deletes an opportunity from ConnectWise and removes the corresponding
* record (along with its associated ProductData) from the local database.
*
* @param identifier - The internal uid (string) or CW opportunity ID (number)
*/
async deleteItem(identifier: string | number): Promise<void> {
const isNumeric =
typeof identifier === "number" || /^\d+$/.test(String(identifier));
const record = await prisma.opportunity.findFirst({
where: isNumeric
? { id: Number(identifier) }
: { uid: identifier as string },
select: { uid: true, id: true },
});
if (!record) {
throw new GenericError({
message: "Opportunity not found",
name: "OpportunityNotFound",
cause: `No opportunity exists with identifier '${identifier}'`,
status: 404,
});
}
await opportunityCw.delete(record.id);
await prisma.$transaction([
prisma.productData.deleteMany({ where: { opportunityId: record.id } }),
prisma.opportunity.delete({ where: { uid: record.uid } }),
]);
},
};