Skip to content

Commit 424470d

Browse files
author
Tereshchenko Aleksandr
committed
WIP: fix Curre Chat Updater to prevent enrolments errors in prod
1 parent 3ef501e commit 424470d

File tree

10 files changed

+45
-15
lines changed

10 files changed

+45
-15
lines changed

src/server/db/models/enrolment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Enrolment.init(
4242
indexes: [
4343
{
4444
unique: true,
45-
fields: ['userId', 'chatInstanceId'],
45+
fields: ['user_id', 'chat_instance_id'],
4646
},
4747
],
4848
}

src/server/db/models/responsibilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Responsibility.init(
4242
indexes: [
4343
{
4444
unique: true,
45-
fields: ['userId', 'chatInstanceId'],
45+
fields: ['user_id', 'chat_instance_id'],
4646
},
4747
],
4848
}

src/server/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ if (inProduction || inStaging) {
3333
app.listen(PORT, async () => {
3434
await connectToDatabase()
3535
await seed()
36-
if (inProduction || inStaging) {
36+
if (true || inProduction || inStaging) {
3737
await setupCron()
3838
}
3939

src/server/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { Request } from 'express'
22
import OpenAI from 'openai'
33
import { ChatRequestMessage, GetChatCompletionsOptions } from '@azure/openai'
44

5+
export type PartialRecord<K extends keyof any, T> = Partial<Record<K, T>>
6+
57
export interface User {
68
id: string
79
username: string

src/server/updater/courses.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,13 @@ const createChatInstance = async (
6060
entities: chatInstances,
6161
bulkCreate: async (e, opts) => ChatInstance.bulkCreate(e, opts),
6262
fallbackCreate: async (e, opts) => ChatInstance.upsert(e, opts),
63-
options: {
63+
bulkCreateOptions: {
6464
updateOnDuplicate: ['name'],
6565
conflictAttributes: ['courseId'],
6666
},
67+
fallbackCreateOptions: {
68+
fields: ['courseId']
69+
}
6770
})
6871
}
6972

src/server/updater/enrolments.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,15 @@ const enrolmentsHandler = async (enrolments: SisuEnrolment[]) => {
3636
entities: toInsert,
3737
bulkCreate: async (e, opt) => Enrolment.bulkCreate(e, opt),
3838
fallbackCreate: async (e, opt) => Enrolment.upsert(e, opt),
39-
options: {
40-
conflictAttributes: ['user_id', 'chat_instance_id'],
39+
bulkCreateOptions: {
40+
conflictAttributes: ['userId', 'chatInstanceId'],
4141
ignoreDuplicates: true,
4242
},
43+
fallbackCreateOptions: {
44+
conflictFields: ['user_id', 'chat_instanceId'],
45+
fields: ['user_id', 'chat_instance_id'],
46+
returning: false,
47+
}
4348
})
4449
}
4550

src/server/updater/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { fetchUsers } from './users'
66
import { clearOffsets } from './util'
77

88
const runUpdater = async () => {
9-
await fetchUsers()
9+
// await fetchUsers()
1010
await fetchCoursesAndResponsibilities()
1111
await fetchEnrolments()
1212
}

src/server/updater/responsibilities.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,15 @@ export const upsertResponsibilities = async (
7878
entities: responsibilitiesToInsert,
7979
bulkCreate: async (e, opts) => Responsibility.bulkCreate(e, opts),
8080
fallbackCreate: async (e, opts) => Responsibility.upsert(e, opts),
81-
options: {
82-
conflictAttributes: ['user_id', 'chat_instance_id'],
81+
bulkCreateOptions: {
82+
conflictAttributes: ['userId', 'chatInstanceId'],
8383
ignoreDuplicates: true,
8484
},
85+
fallbackCreateOptions: {
86+
conflictFields: ['user_id', 'chat_instance_id'],
87+
fields: ['user_id', 'chat_instance_id'],
88+
returning: false,
89+
}
8590
})
8691

8792
await t.commit()

src/server/updater/users.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ const usersHandler = async (users: SisuUser[]) => {
3333
entities: parsedUsers,
3434
bulkCreate: async (e, opt) => User.bulkCreate(e, opt),
3535
fallbackCreate: async (e, opt) => User.upsert(e, opt),
36-
options: {
36+
bulkCreateOptions: {
3737
updateOnDuplicate: ['language', 'username'],
3838
},
39+
fallbackCreateOptions: {
40+
fields: ['language', 'username'],
41+
}
3942
})
4043
}
4144

src/server/updater/util.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,42 @@ import * as Sentry from '@sentry/node'
33

44
import { redis } from '../util/redis'
55
import logger from '../util/logger'
6+
import { PartialRecord } from '../types'
67

78
const logError = (message: string, error: Error) => {
89
logger.error(`${message} ${error.name}, ${error.message}`)
910

1011
Sentry.captureException(error)
1112
}
1213

14+
type AllowedBulkCreateOptionField =
15+
| 'conflictAttributes'
16+
| 'updateOnDuplicate'
17+
| 'ignoreDuplicates'
18+
type AllowedFallbackCreateOptionField =
19+
| 'fields'
20+
| 'conflictFields'
21+
1322
interface BulkCreateOptions {
1423
entityName: string
1524
bulkCreate: (entities: object[], options: any) => Promise<any>
1625
fallbackCreate: (entity: object, options: any) => Promise<any>
17-
options: Record<string, any>
26+
bulkCreateOptions: PartialRecord<AllowedBulkCreateOptionField, any>
27+
fallbackCreateOptions: PartialRecord<AllowedFallbackCreateOptionField, any>
1828
entities: Record<string, any>[]
1929
}
2030
export const safeBulkCreate = async ({
2131
entityName,
2232
bulkCreate,
2333
fallbackCreate,
24-
options,
34+
bulkCreateOptions,
35+
fallbackCreateOptions,
2536
entities,
2637
}: BulkCreateOptions) => {
2738
try {
28-
const result = await bulkCreate(entities, options)
39+
const result = await bulkCreate(entities, {
40+
...bulkCreateOptions,
41+
})
2942
return result
3043
} catch (bulkCreateError: any) {
3144
const result = []
@@ -38,8 +51,7 @@ export const safeBulkCreate = async ({
3851
try {
3952
// eslint-disable-next-line no-await-in-loop
4053
const res = await fallbackCreate(entity, {
41-
...options,
42-
fields: options.updateOnDuplicate,
54+
...fallbackCreateOptions,
4355
})
4456
result.push(res)
4557
} catch (fallbackCreateError: any) {

0 commit comments

Comments
 (0)