Skip to content

Commit fe47cd1

Browse files
committed
feat(Support): ensure tickets added to search index immed after create or update
1 parent b60979f commit fe47cd1

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

server/api/services/support-ticket-service.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,40 @@ export default class SupportTicketService {
303303
}
304304
}
305305

306+
/**
307+
* This method can be called after any update to a ticket to ensure the search index has the most up-to-date information.
308+
* This way, we don't have to wait for the next full sync to have accurate search results after a ticket is updated.
309+
* Essentially does the same thing as syncWithSearchIndex but for a single ticket, and is optimized to only reindex that one ticket instead of all tickets.
310+
* Will swallow and log any errors instead of throwing, since this is meant to be a best-effort method to keep the search index up-to-date.
311+
* @param ticketID the ID of the ticket to upsert to the search index
312+
*/
313+
async upsertToSearchIndex(ticketID: string): Promise<void> {
314+
try {
315+
const searchService = await SearchService.create();
316+
317+
const results = await SupportTicket.aggregate([
318+
{ $match: { uuid: ticketID } },
319+
...this.lookupUserDataStages("assignedUUIDs"),
320+
...this.lookupUserDataStages("userUUID"),
321+
]);
322+
323+
if (!results || results.length === 0) {
324+
debugError(`[SupportTicketService] No ticket found with ID ${ticketID} for upsert to search index.`);
325+
return;
326+
}
327+
328+
const ticket = results[0] as SupportTicketInterface;
329+
if (!ticket) {
330+
debugError(`[SupportTicketService] No ticket found with ID ${ticketID} for upsert to search index.`);
331+
return;
332+
}
333+
334+
await searchService.addDocuments("supportTickets", [ticket]);
335+
} catch (err) {
336+
debugError(`[SupportTicketService] Error upserting ticket ${ticketID} to search index: ${err}`);
337+
}
338+
}
339+
306340
_removeAccessKeysFromResponse(
307341
ticket: SupportTicketInterface,
308342
allowGuestAccessKey = false,

server/api/support.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,8 @@ async function assignTicket(
447447
assigner,
448448
});
449449

450+
await ticketService.upsertToSearchIndex(ticket.uuid);
451+
450452
return res.send({
451453
err: false,
452454
ticket: ticketService._removeAccessKeysFromResponse(ticket),
@@ -765,6 +767,8 @@ async function createTicket(
765767
});
766768
}
767769

770+
await ticketService.upsertToSearchIndex(ticket.uuid);
771+
768772
return res.send({
769773
err: false,
770774
ticket: ticketService._removeAccessKeysFromResponse(ticket, true), // Allow guest access key to be returned here for attachments to be immediately uploaded
@@ -985,6 +989,8 @@ async function updateTicket(
985989
},
986990
).orFail();
987991

992+
await ticketService.upsertToSearchIndex(uuid);
993+
988994
return res.send({
989995
err: false,
990996
ticket: ticketService._removeAccessKeysFromResponse(ticket),

0 commit comments

Comments
 (0)