feat : update i guess
This commit is contained in:
@@ -40,7 +40,8 @@ describe("commission message deletion", () => {
|
||||
vi.clearAllMocks();
|
||||
authorizeTicket.mockResolvedValue({ ticket: { userId: ownerId, status: "open" }, user: { id: ownerId } });
|
||||
deleteWhere.mockImplementation(() => ({ returning: deleteReturning }));
|
||||
deleteReturning.mockResolvedValue([{ imageObjectKey: "commission/messages/image-1" }]);
|
||||
deleteReturning.mockResolvedValue([{ imageObjectKey: "commission/messages/image-1",
|
||||
imageObjectKeys: ["commission/messages/image-2", "commission/messages/image-3"] }]);
|
||||
updateWhere.mockResolvedValue(undefined);
|
||||
deleteObject.mockResolvedValue(undefined);
|
||||
notifyCommission.mockResolvedValue(undefined);
|
||||
@@ -63,6 +64,8 @@ describe("commission message deletion", () => {
|
||||
expect(query.params).toContain(messageId);
|
||||
expect(query.params).toContain(ownerId);
|
||||
expect(deleteObject).toHaveBeenCalledWith("commission/messages/image-1");
|
||||
expect(deleteObject).toHaveBeenCalledWith("commission/messages/image-2");
|
||||
expect(deleteObject).toHaveBeenCalledWith("commission/messages/image-3");
|
||||
expect(notifyCommission).toHaveBeenCalledWith(ticketId, ownerId);
|
||||
});
|
||||
|
||||
|
||||
@@ -17,16 +17,20 @@ export async function DELETE(request: Request,
|
||||
const { ticket, user } = await authorizeTicket(id);
|
||||
if (ticket.status !== "open") throw new HttpError(409, "ticket-closed");
|
||||
await limitRequest("commission-message-delete", user.id, 30);
|
||||
const imageObjectKey = await getDb().transaction(async (tx) => {
|
||||
const imageObjectKeys = await getDb().transaction(async (tx) => {
|
||||
const [message] = await tx.delete(commissionMessages)
|
||||
.where(and(eq(commissionMessages.id, messageId), eq(commissionMessages.ticketId, id),
|
||||
eq(commissionMessages.authorId, user.id)))
|
||||
.returning({ imageObjectKey: commissionMessages.imageObjectKey });
|
||||
.returning({ imageObjectKey: commissionMessages.imageObjectKey,
|
||||
imageObjectKeys: commissionMessages.imageObjectKeys });
|
||||
if (!message) throw new HttpError(404, "message-not-found");
|
||||
await tx.update(commissionTickets).set({ updatedAt: new Date() }).where(eq(commissionTickets.id, id));
|
||||
return message.imageObjectKey;
|
||||
return [message.imageObjectKey, ...message.imageObjectKeys].filter((key): key is string => Boolean(key));
|
||||
});
|
||||
if (imageObjectKey) await (await getMediaStorage()).delete(imageObjectKey).catch(() => undefined);
|
||||
if (imageObjectKeys.length) {
|
||||
const storage = await getMediaStorage();
|
||||
await Promise.all(imageObjectKeys.map((key) => storage.delete(key).catch(() => undefined)));
|
||||
}
|
||||
await notifyCommission(id, ticket.userId);
|
||||
return new Response(null, { status: 204 });
|
||||
} catch (cause) { return errorResponse(cause); }
|
||||
|
||||
Reference in New Issue
Block a user