diff --git a/server/pnpm-lock.yaml b/server/pnpm-lock.yaml index b1e9a7a31..806513d3b 100644 --- a/server/pnpm-lock.yaml +++ b/server/pnpm-lock.yaml @@ -62,7 +62,7 @@ dependencies: express-async-errors: 3.1.1_express@4.17.1 express-session: 1.17.1 fast-json-stable-hash: 1.0.1 - kamaitachi-common: github.com/zkldi/Kamaitachi-common/927113e61ae262678e1890df8c03d7491ccd4ee6 + kamaitachi-common: github.com/zkldi/Kamaitachi-common/b63c3213ba997b06d87f72e8dc18dde061502e6c mongodb: 3.6.6 monk: 7.3.4 multer: 1.4.2 @@ -4638,8 +4638,8 @@ packages: '@types/yoga-layout': 1.9.2 dev: true - github.com/zkldi/Kamaitachi-common/927113e61ae262678e1890df8c03d7491ccd4ee6: - resolution: {tarball: https://codeload.github.com/zkldi/Kamaitachi-common/tar.gz/927113e61ae262678e1890df8c03d7491ccd4ee6} + github.com/zkldi/Kamaitachi-common/b63c3213ba997b06d87f72e8dc18dde061502e6c: + resolution: {tarball: https://codeload.github.com/zkldi/Kamaitachi-common/tar.gz/b63c3213ba997b06d87f72e8dc18dde061502e6c} name: kamaitachi-common version: 0.1.0 dependencies: diff --git a/server/scripts/__KT_DATABASE_MIGRATION/goals.ts b/server/scripts/__KT_DATABASE_MIGRATION/goals.ts new file mode 100644 index 000000000..13da3e694 --- /dev/null +++ b/server/scripts/__KT_DATABASE_MIGRATION/goals.ts @@ -0,0 +1,78 @@ +// oh boy + +import { GoalDocument } from "kamaitachi-common"; +import db from "../../src/db/db"; +import CreateLogCtx from "../../src/logger"; +import MigrateRecords from "./migrate"; + +const logger = CreateLogCtx("goals.ts"); + +function ConvertFn(c: any): GoalDocument | null { + let charts: GoalDocument["charts"]; + + if (c.directChartID) { + charts = { + type: "single", + data: c.directChartID, + }; + } else if (c.directChartIDs) { + charts = { + type: "multi", + data: c.directChartIDs, + }; + } else if (c.chartQuery) { + logger.warn(`Skipped goal ${c.title}, as it used cQuery.`); + return null; + } else { + logger.warn(`Any Goal ${c.goalID}, ${c.title}. Skipping.`); + return null; + } + + let partialCriteria: Partial = { + mode: "single", + }; + + if (c.scoreQuery["scoreData¬lampIndex"]) { + partialCriteria.key = "scoreData.lampIndex"; + } else if (c.scoreQuery["scoreData¬gradeIndex"]) { + partialCriteria.key = "scoreData.gradeIndex"; + } else if (c.scoreQuery["scoreData¬score"]) { + partialCriteria.key = "scoreData.score"; + } else if (c.scoreQuery["scoreData¬percent"]) { + partialCriteria.key = "scoreData.percent"; + } else { + throw new Error(`Invalid goal ${c.goalID} ${c.title}.`); + } + + const v = c.scoreQuery[partialCriteria.key.replace(/\./gu, "¬")]; + + const value = v ? v["~gte"] : undefined; + + // would break for value == 0, but a goal would never be + // zero. + if (!value) { + throw new Error(`Invalid goal ${c.goalID} ${c.title}.`); + } + + partialCriteria.value = value; + + // @ts-expect-error ts cant resolve this union properly + const newGoalDoc: GoalDocument = { + game: c.game, + playtype: c.playtype, + title: c.title, + timeAdded: c.timeAdded, + goalID: c.goalID, // maybe + createdBy: c.createdBy, + charts, + criteria: partialCriteria as GoalDocument["criteria"], + }; + + return newGoalDoc; +} + +(async () => { + await MigrateRecords(db.goals, "goals", ConvertFn); + + process.exit(0); +})(); diff --git a/server/scripts/__KT_DATABASE_MIGRATION/user-goals.ts b/server/scripts/__KT_DATABASE_MIGRATION/user-goals.ts new file mode 100644 index 000000000..fe8277ddf --- /dev/null +++ b/server/scripts/__KT_DATABASE_MIGRATION/user-goals.ts @@ -0,0 +1,26 @@ +// oh boy + +import { UserGoalDocument } from "kamaitachi-common"; +import db from "../../src/db/db"; +import CreateLogCtx from "../../src/logger"; +import MigrateRecords from "./migrate"; + +const logger = CreateLogCtx("user-goals.ts"); + +async function ConvertFn(c: any): Promise { + // const newUG: UserGoalDocument = {}; + + let relatedGoal = await db.goals.findOne({ goalID: c.goalID }); + + if (!relatedGoal) { + return null; + } + + return c; +} + +(async () => { + await MigrateRecords(db["user-goals"], "user-goals", ConvertFn); + + process.exit(0); +})(); diff --git a/server/scripts/set-indexes.ts b/server/scripts/set-indexes.ts index 06064ef58..4be48c517 100644 --- a/server/scripts/set-indexes.ts +++ b/server/scripts/set-indexes.ts @@ -53,6 +53,8 @@ const indexes: Partial> = { index({ chartID: 1, tierlistID: 1, type: 1 }), index({ chartID: 1, tierlistID: 1, type: 1, key: 1 }, UNIQUE), ], + goals: [index({ goalID: 1 }, UNIQUE)], + "user-goals": [index({ goalID: 1, userID: 1 }, UNIQUE), index({ goalID: 1 })], }; (async () => { diff --git a/server/scripts/tsconfig.json b/server/scripts/tsconfig.json new file mode 100644 index 000000000..998cdf3ae --- /dev/null +++ b/server/scripts/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "../tsconfig.json", + "include": [ + "./**/*.ts", + ], +} \ No newline at end of file