Skip to content

Commit

Permalink
Update totalTastings upon deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
dcramer committed May 21, 2023
1 parent 300f850 commit d610255
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 3 additions & 1 deletion apps/api/src/routes/deleteComment.ts
Expand Up @@ -38,12 +38,14 @@ export default {
.update(tastings)
.set({ comments: sql`${tastings.comments} - 1` })
.where(eq(tastings.id, comment.tastingId));
await tx.delete(comments).where(eq(comments.id, comment.id));

await deleteNotification(tx, {
objectType: objectTypeFromSchema(comments),
objectId: comment.id,
userId: comment.createdById,
});

await tx.delete(comments).where(eq(comments.id, comment.id));
});
res.status(204).send();
},
Expand Down
10 changes: 9 additions & 1 deletion apps/api/src/routes/deleteTasting.ts
Expand Up @@ -2,7 +2,7 @@ import { and, eq, inArray, sql } from "drizzle-orm";
import type { RouteOptions } from "fastify";
import { IncomingMessage, Server, ServerResponse } from "http";
import { db } from "../db";
import { notifications, tastings, toasts } from "../db/schema";
import { bottles, notifications, tastings, toasts } from "../db/schema";
import { objectTypeFromSchema } from "../lib/notifications";
import { requireAuth } from "../middleware/auth";

Expand Down Expand Up @@ -47,6 +47,14 @@ export default {
);
await tx.delete(toasts).where(eq(toasts.tastingId, tasting.id));
await tx.delete(tastings).where(eq(tastings.id, tasting.id));

// update stats
await tx
.update(bottles)
.set({
totalTastings: sql<number>`${bottles.totalTastings} - 1`,
})
.where(eq(bottles.id, tasting.bottleId));
});
res.status(204).send();
},
Expand Down

0 comments on commit d610255

Please sign in to comment.