Skip to content

Commit 4df68c8

Browse files
committed
feat(next/api): update sync range logic
1 parent 2c3a411 commit 4df68c8

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

next/api/src/interfaces/ticket.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export type SyncTicketSearchDocumentJobData =
4343
type: 'syncByRange';
4444
start?: string;
4545
end?: string;
46+
exclude?: string[];
4647
limit?: number;
4748
delay?: number;
4849
};

next/api/src/service/search-ticket.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,30 +151,38 @@ export class SearchTicketService {
151151
private async processSyncByRangeJob(
152152
data: Extract<SyncTicketSearchDocumentJobData, { type: 'syncByRange' }>
153153
) {
154-
const { start, end, limit = 100, delay = 1000 } = data;
154+
const { start, end, exclude, limit = 100, delay = 1000 } = data;
155+
155156
const query = Ticket.queryBuilder();
156157
if (start) {
157-
query.where('createdAt', '>', new Date(start));
158+
query.where('createdAt', '>=', new Date(start));
158159
}
159160
if (end) {
160-
query.where('createdAt', '<', new Date(end));
161+
query.where('createdAt', '<=', new Date(end));
162+
}
163+
if (exclude?.length) {
164+
query.where('objectId', 'not-in', exclude);
161165
}
166+
162167
const tickets = await query.orderBy('createdAt').limit(limit).find({ useMasterKey: true });
163168
if (tickets.length === 0) {
164169
console.log('[SearchTicketService] Sync job is done');
165170
return;
166171
}
167172
await this.syncTickets(tickets);
168-
const lastCreatedAt = tickets[tickets.length - 1].createdAt.toISOString();
169-
console.log(
170-
`[SearchTicketService] Sync ${
171-
tickets.length
172-
} tickets, (${tickets[0].createdAt.toISOString()},${lastCreatedAt})`
173-
);
173+
174+
const lastCreatedAt = tickets[tickets.length - 1].createdAt;
175+
176+
// prettier-ignore
177+
console.log(`[SearchTicketService] Sync ${tickets.length} tickets, (${tickets[0].createdAt.toISOString()},${lastCreatedAt.toISOString()})`);
178+
174179
await this.syncQueue?.add(
175180
{
176181
...data,
177-
start: lastCreatedAt,
182+
start: lastCreatedAt.toISOString(),
183+
exclude: tickets
184+
.filter((ticket) => ticket.createdAt.getTime() === lastCreatedAt.getTime())
185+
.map((ticket) => ticket.id),
178186
},
179187
{
180188
delay,
@@ -300,7 +308,7 @@ export class SearchTicketService {
300308
.toJSON();
301309

302310
const res = await this.esClient.search({
303-
index: [this.indexName, this.indexName + '-tmp'],
311+
index: this.indexName,
304312
body,
305313
});
306314

0 commit comments

Comments
 (0)