notif after complaint resolution
This commit is contained in:
parent
0290c170bb
commit
5642c53462
5 changed files with 60 additions and 3 deletions
|
|
@ -1,7 +1,13 @@
|
|||
import { router, protectedProcedure } from '@/src/trpc/trpc-index'
|
||||
import { z } from 'zod';
|
||||
import { generateSignedUrlsFromS3Urls } from '@/src/lib/s3-client'
|
||||
import { getComplaints as getComplaintsFromDb, resolveComplaint as resolveComplaintInDb } from '@/src/dbService'
|
||||
import { queueDataPusher } from '@/src/lib/queue-data-pusher'
|
||||
import {
|
||||
getComplaintById,
|
||||
getComplaints as getComplaintsFromDb,
|
||||
getNotifTokensByUserIds,
|
||||
resolveComplaint as resolveComplaintInDb,
|
||||
} from '@/src/dbService'
|
||||
import type { ComplaintWithUser } from '@packages/shared'
|
||||
|
||||
export const complaintRouter = router({
|
||||
|
|
@ -90,10 +96,43 @@ export const complaintRouter = router({
|
|||
}),
|
||||
|
||||
resolve: protectedProcedure
|
||||
.input(z.object({ id: z.string(), response: z.string().optional() }))
|
||||
.input(z.object({ id: z.string(), response: z.string().min(1) }))
|
||||
.mutation(async ({ input }): Promise<{ message: string }> => {
|
||||
const complaintId = parseInt(input.id)
|
||||
const responseText = input.response.trim()
|
||||
|
||||
const complaint = await getComplaintById(complaintId)
|
||||
|
||||
if (!complaint) {
|
||||
throw new Error('Complaint not found')
|
||||
}
|
||||
|
||||
// Using dbService helper (new implementation)
|
||||
await resolveComplaintInDb(parseInt(input.id), input.response);
|
||||
await resolveComplaintInDb(complaintId, responseText)
|
||||
|
||||
const tokens = await getNotifTokensByUserIds([complaint.userId])
|
||||
for (const t of tokens) {
|
||||
try {
|
||||
await queueDataPusher.pushNotifQueue({
|
||||
name: 'send-admin-notification',
|
||||
jobData: {
|
||||
token: t.token,
|
||||
title: 'Complaint Response Received',
|
||||
body: responseText,
|
||||
imageUrl: null,
|
||||
},
|
||||
options: {
|
||||
attempts: 3,
|
||||
backoff: {
|
||||
type: 'exponential',
|
||||
delay: 2000,
|
||||
},
|
||||
},
|
||||
})
|
||||
} catch (e) {
|
||||
console.error('Failed to queue complaint response notification', e)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
// Old implementation - direct DB query:
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ export {
|
|||
export {
|
||||
// Complaint
|
||||
getComplaints,
|
||||
getComplaintById,
|
||||
resolveComplaint,
|
||||
} from './src/admin-apis/complaint';
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,14 @@ export interface ComplaintWithUser extends Complaint {
|
|||
userMobile: string | null;
|
||||
}
|
||||
|
||||
export async function getComplaintById(id: number): Promise<Complaint | null> {
|
||||
const complaint = await db.query.complaints.findFirst({
|
||||
where: eq(complaints.id, id),
|
||||
});
|
||||
|
||||
return complaint ?? null;
|
||||
}
|
||||
|
||||
export async function getComplaints(
|
||||
cursor?: number,
|
||||
limit: number = 20
|
||||
|
|
|
|||
|
|
@ -22,6 +22,7 @@ export {
|
|||
export {
|
||||
// Complaint
|
||||
getComplaints,
|
||||
getComplaintById,
|
||||
resolveComplaint,
|
||||
} from './src/admin-apis/complaint'
|
||||
|
||||
|
|
|
|||
|
|
@ -18,6 +18,14 @@ export interface ComplaintWithUser extends Complaint {
|
|||
userMobile: string | null
|
||||
}
|
||||
|
||||
export async function getComplaintById(id: number): Promise<Complaint | null> {
|
||||
const complaint = await db.query.complaints.findFirst({
|
||||
where: eq(complaints.id, id),
|
||||
})
|
||||
|
||||
return complaint ?? null
|
||||
}
|
||||
|
||||
export async function getComplaints(
|
||||
cursor?: number,
|
||||
limit: number = 20
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue