feat: fix and add several things
This commit is contained in:
@@ -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 } }),
|
||||
]);
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user