Skip to content

Commit

Permalink
feat: add self reaction on response
Browse files Browse the repository at this point in the history
  • Loading branch information
arsaizdihar committed Jun 1, 2024
1 parent 0496ffb commit 79512aa
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/controllers/comment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const commentRouter = createAuthRouter();

commentRouter.openapi(getCommentsListRoute, async (c) => {
const query = c.req.valid('query');
const comment = await getCommentList(db, query);
const comment = await getCommentList(db, query, c.var.user.id);
return c.json({ comment }, 200);
});

Expand Down
10 changes: 7 additions & 3 deletions src/controllers/reaction.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createAuthRouter } from './router-factory';
import { db } from '~/db/drizzle';
import {
createOrUpdateReaction,
deleteReaction,
Expand All @@ -9,13 +9,17 @@ import {
deleteReactionRoute,
getReactionsRoute,
} from '~/routes/reaction.route';
import { db } from '~/db/drizzle';
import { createAuthRouter } from './router-factory';

export const reactionRouter = createAuthRouter();

reactionRouter.openapi(getReactionsRoute, async (c) => {
try {
const reactions = await getReactions(db, c.req.valid('query'));
const reactions = await getReactions(
db,
c.req.valid('query'),
c.var.user.id,
);
if (!reactions) return c.json({ error: 'No reactions found' }, 404);
return c.json(reactions, 200);
} catch (err) {
Expand Down
2 changes: 2 additions & 0 deletions src/repositories/comment.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { getCommentsReactions } from './reaction.repo';
export async function getCommentList(
db: Database,
q: z.infer<typeof CommentListQuerySchema>,
userId: string,
) {
const infoIdQ = q.infoId ? eq(comments.repliedInfoId, q.infoId) : undefined;
const sortQ =
Expand All @@ -31,6 +32,7 @@ export async function getCommentList(
const reactions = await getCommentsReactions(
db,
result.map((r) => r.id),
userId,
);
return result.map((r) => ({
...r,
Expand Down
26 changes: 24 additions & 2 deletions src/repositories/reaction.repo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { count, eq, inArray } from 'drizzle-orm';
import { and, count, eq, inArray } from 'drizzle-orm';
import { z } from 'zod';
import { Database } from '~/db/drizzle';
import { first, firstSure } from '~/db/helper';
Expand All @@ -12,6 +12,7 @@ import {
export async function getReactions(
db: Database,
q: z.infer<typeof ReactionQuerySchema>,
userId: string,
) {
const where = q.infoId
? eq(reactions.infoId, q.infoId)
Expand All @@ -27,10 +28,18 @@ export async function getReactions(
.from(reactions)
.where(where)
.groupBy(reactions.reaction);
const userReaction = await db
.select({
reaction: reactions.reaction,
})
.from(reactions)
.where(and(eq(reactions.creatorId, userId), where))
.then(first);

const result: z.infer<typeof ReactionResponseSchema> = {
totalReactions: reactionCount.reduce((acc, r) => acc + r.count, 0),
reactionsCount: reactionCount,
userReaction: userReaction?.reaction,
};

return result;
Expand Down Expand Up @@ -67,7 +76,11 @@ export async function deleteReaction(db: Database, id: string) {
/**
* Get batch of comments reactions
*/
export async function getCommentsReactions(db: Database, commentIds: string[]) {
export async function getCommentsReactions(
db: Database,
commentIds: string[],
userId: string,
) {
const where = inArray(reactions.commentId, commentIds);

const reactionCount = await db
Expand All @@ -79,6 +92,13 @@ export async function getCommentsReactions(db: Database, commentIds: string[]) {
.from(reactions)
.where(where)
.groupBy(reactions.reaction, reactions.commentId);
const userReactions = await db
.select({
reaction: reactions.reaction,
commentId: reactions.commentId,
})
.from(reactions)
.where(and(eq(reactions.creatorId, userId), where));

const result: Record<string, z.infer<typeof ReactionResponseSchema>> = {};
reactionCount.forEach((r) => {
Expand All @@ -89,6 +109,8 @@ export async function getCommentsReactions(db: Database, commentIds: string[]) {
result[r.commentId] = {
totalReactions: 0,
reactionsCount: [],
userReaction: userReactions.find((ur) => ur.commentId === r.commentId)
?.reaction,
};
}
result[r.commentId].totalReactions += r.count;
Expand Down
1 change: 1 addition & 0 deletions src/types/reaction.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export const ReactionResponseSchema = z
.object({
totalReactions: z.number(),
reactionsCount: ReactionCountSchema,
userReaction: z.string().optional(),
})
.openapi('ReactionAggregate');

Expand Down

0 comments on commit 79512aa

Please sign in to comment.