Skip to content

Commit

Permalink
fix: sort graph when query db
Browse files Browse the repository at this point in the history
  • Loading branch information
fibonacci998 committed Aug 30, 2023
1 parent 030ee48 commit a1e06b9
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/services/job/reassign_msg_index_to_event.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export default class JobReAssignMsgIndexToEvent extends BullableService {
await knex.transaction(async (trx) => {
const listTx = await Transaction.query()
.withGraphFetched('events.[attributes]')
.modifyGraph('events', (builder) => {
builder.orderBy('id', 'asc');
})
.modifyGraph('events.[attributes]', (builder) => {
builder.orderBy('index', 'asc');
})
.orderBy('id', 'asc')
.where('height', '>=', blockCheckpoint?.height ?? 0)
.andWhere('height', '<', lastBlock)
Expand All @@ -69,27 +75,26 @@ export default class JobReAssignMsgIndexToEvent extends BullableService {
.transacting(trx)
);

const events = tx.events.sort((a: any, b: any) => a.id - b.id);
events.forEach((event: any, index: number) => {
tx.events.forEach((event: any, index: number) => {
const rawEvents = rawData.tx_response.events;
// check if event in raw is the same as event in db
if (rawEvents[index].type === event.type) {
const attributes = event.attributes.sort(
(a: any, b: any) => a.index - b.index
const checkIndex = event.attributes.every(
(attr: EventAttribute) => {
const decodedKey = rawEvents[index].attributes[attr.index].key
? fromUtf8(
fromBase64(rawEvents[index].attributes[attr.index].key)
)
: null;
const decodedValue = rawEvents[index].attributes[attr.index]
.value
? fromUtf8(
fromBase64(rawEvents[index].attributes[attr.index].value)
)
: null;
return attr.key === decodedKey && attr.value === decodedValue;
}
);
const checkIndex = attributes.every((attr: EventAttribute) => {
const decodedKey = rawEvents[index].attributes[attr.index].key
? fromUtf8(
fromBase64(rawEvents[index].attributes[attr.index].key)
)
: null;
const decodedValue = rawEvents[index].attributes[attr.index].value
? fromUtf8(
fromBase64(rawEvents[index].attributes[attr.index].value)
)
: null;
return attr.key === decodedKey && attr.value === decodedValue;
});
if (!checkIndex) {
throw new Error('order attribute is wrong');
} else {
Expand Down

0 comments on commit a1e06b9

Please sign in to comment.