diff --git a/.eslintrc b/.eslintrc index d39773ad2..bf3e9573d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -15,7 +15,8 @@ "rules": { // broken "lines-around-comment": "off", - + // no idea why this is on + "no-param-reassign": "off", // TENPORARILY OFF AS THEY'RE BROKEN. "@typescript-eslint/no-unsafe-assignment": "off", "@typescript-eslint/no-unsafe-return": "off", diff --git a/server/src/lib/score-import/framework/score-importing/score-import-main.ts b/server/src/lib/score-import/framework/score-importing/score-import-main.ts index 5dbf27549..9b073e407 100644 --- a/server/src/lib/score-import/framework/score-importing/score-import-main.ts +++ b/server/src/lib/score-import/framework/score-importing/score-import-main.ts @@ -129,20 +129,21 @@ export default async function ScoreImportMain( job ); } catch (err) { - logger.error( - `An error was thrown from ImportAllIterableData, which has resulted in a potential partial-score-import. Undoing scores inserted from this import.`, - { err } - ); - // Remove all scores from the database for this user which were imported after our timer started. const r = await db.scores.remove({ userID: user.id, timeAdded: { $gte: startOfImportingScores }, }); - logger.error( - `Removed ${r.deletedCount} scores from the database to undo partial-import.` - ); + if (r.deletedCount !== 0) { + logger.error( + `An error was thrown from ImportAllIterableData, which has resulted in a potential partial-score-import. Undoing scores inserted from this import.`, + { err } + ); + logger.error( + `Removed ${r.deletedCount} scores from the database to undo partial-import.` + ); + } throw err; } diff --git a/server/src/lib/score-import/import-types/common/api-kai/iidx/parser.test.ts b/server/src/lib/score-import/import-types/common/api-kai/iidx/parser.test.ts index c1c3563d0..ae7f0d48c 100644 --- a/server/src/lib/score-import/import-types/common/api-kai/iidx/parser.test.ts +++ b/server/src/lib/score-import/import-types/common/api-kai/iidx/parser.test.ts @@ -13,6 +13,8 @@ const fakeAuth = { const logger = CreateLogCtx(__filename); +const NO_REAUTH = () => Promise.resolve(""); + t.test("#ParseKaiIIDX", (t) => { t.beforeEach(ResetDBState); @@ -32,7 +34,7 @@ t.test("#ParseKaiIIDX", (t) => { }, }); - const res = await ParseKaiIIDX("FLO", fakeAuth, logger, mockFloAPI); + const res = await ParseKaiIIDX("FLO", fakeAuth, logger, mockFloAPI, NO_REAUTH); t.equal(res.game, "iidx"); t.strictSame(res.context, { service: "FLO" }); @@ -64,7 +66,7 @@ t.test("#ParseKaiIIDX", (t) => { }, }); - const res = await ParseKaiIIDX("EAG", fakeAuth, logger, mockEagAPI); + const res = await ParseKaiIIDX("EAG", fakeAuth, logger, mockEagAPI, NO_REAUTH); t.equal(res.game, "iidx"); t.strictSame(res.context, { service: "EAG" }); diff --git a/server/src/lib/score-import/import-types/common/api-kai/iidx/parser.ts b/server/src/lib/score-import/import-types/common/api-kai/iidx/parser.ts index 625f86e3b..6eed8a1f9 100644 --- a/server/src/lib/score-import/import-types/common/api-kai/iidx/parser.ts +++ b/server/src/lib/score-import/import-types/common/api-kai/iidx/parser.ts @@ -12,11 +12,17 @@ export async function ParseKaiIIDX( service: "EAG" | "FLO", authDoc: KaiAuthDocument, logger: KtLogger, - fetch = nodeFetch + fetch = nodeFetch, + reauthFn: (() => Promise) | null = null ): Promise> { const baseUrl = KaiTypeToBaseURL(service); - const reauthFn = CreateKaiReauthFunction(service, authDoc, logger, fetch); + if (!reauthFn) { + reauthFn = CreateKaiReauthFunction(service, authDoc, logger, fetch); + } + + // auth *before* starting import to avoid a partial-import + authDoc.token = await reauthFn(); return { iterable: TraverseKaiAPI( diff --git a/server/src/lib/score-import/import-types/common/api-kai/sdvx/parser.test.ts b/server/src/lib/score-import/import-types/common/api-kai/sdvx/parser.test.ts index ee4f069e9..7fc32f462 100644 --- a/server/src/lib/score-import/import-types/common/api-kai/sdvx/parser.test.ts +++ b/server/src/lib/score-import/import-types/common/api-kai/sdvx/parser.test.ts @@ -13,6 +13,8 @@ const fakeAuth = { const logger = CreateLogCtx(__filename); +const NO_REAUTH = () => Promise.resolve(""); + t.test("#ParseKaiSDVX", (t) => { t.beforeEach(ResetDBState); @@ -32,7 +34,7 @@ t.test("#ParseKaiSDVX", (t) => { }, }); - const res = await ParseKaiSDVX("FLO", fakeAuth, logger, mockFloAPI); + const res = await ParseKaiSDVX("FLO", fakeAuth, logger, mockFloAPI, NO_REAUTH); t.equal(res.game, "sdvx"); t.strictSame(res.context, { service: "FLO" }); @@ -64,7 +66,7 @@ t.test("#ParseKaiSDVX", (t) => { }, }); - const res = await ParseKaiSDVX("EAG", fakeAuth, logger, mockEagAPI); + const res = await ParseKaiSDVX("EAG", fakeAuth, logger, mockEagAPI, NO_REAUTH); t.equal(res.game, "sdvx"); t.strictSame(res.context, { service: "EAG" }); diff --git a/server/src/lib/score-import/import-types/common/api-kai/sdvx/parser.ts b/server/src/lib/score-import/import-types/common/api-kai/sdvx/parser.ts index 2386c0039..51d1491d4 100644 --- a/server/src/lib/score-import/import-types/common/api-kai/sdvx/parser.ts +++ b/server/src/lib/score-import/import-types/common/api-kai/sdvx/parser.ts @@ -12,11 +12,17 @@ export async function ParseKaiSDVX( service: "EAG" | "FLO" | "MIN", authDoc: KaiAuthDocument, logger: KtLogger, - fetch = nodeFetch + fetch = nodeFetch, + reauthFn: (() => Promise) | null = null ): Promise> { const baseUrl = KaiTypeToBaseURL(service); - const reauthFn = CreateKaiReauthFunction(service, authDoc, logger, fetch); + if (!reauthFn) { + reauthFn = CreateKaiReauthFunction(service, authDoc, logger, fetch); + } + + // auth *before* starting import to avoid a partial-import + authDoc.token = await reauthFn(); return { iterable: TraverseKaiAPI(