Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

fix: update challenge script to use author metadata

bautistaaac6dfa02b673e
diff --git a/packages/db/seed/data/challenge-ingest.ts b/packages/db/seed/data/challenge-ingest.ts
index ae4a1e5d..ef610aff 100644
--- a/packages/db/seed/data/challenge-ingest.ts
+++ b/packages/db/seed/data/challenge-ingest.ts
@@ -3,7 +3,7 @@ import fs from 'node:fs';
import path from 'node:path';
export async function ingestChallenges(challengePath: string) {
- const challengesToCreate: Prisma.ChallengeCreateManyInput[] = [];
+ const challengesToCreate: (Prisma.ChallengeCreateManyInput & { author: string })[] = [];
try {
const items = await fs.promises.readdir(challengePath);
@@ -33,9 +33,9 @@ export async function ingestChallenges(challengePath: string) {
}
async function buildChallenge(pathToDirectory: string) {
- const challengeToCreate: Prisma.ChallengeCreateManyInput = {
+ const challengeToCreate: Prisma.ChallengeCreateManyInput & { author: string } = {
status: 'ACTIVE',
- } as Prisma.ChallengeCreateManyInput;
+ } as Prisma.ChallengeCreateManyInput & { author: string };
const files = await fs.promises.readdir(pathToDirectory);
@@ -83,6 +83,7 @@ async function buildChallenge(pathToDirectory: string) {
challengeToCreate.name = jsonData.label;
challengeToCreate.slug = jsonData.id;
challengeToCreate.shortDescription = jsonData.description;
+ challengeToCreate.author = jsonData.author;
} catch (jsonError) {
console.error('Error parsing JSON:', jsonError);
}
diff --git a/packages/db/temp/update-challenges.ts b/packages/db/temp/update-challenges.ts
index 0571c08a..3f57c361 100644
--- a/packages/db/temp/update-challenges.ts
+++ b/packages/db/temp/update-challenges.ts
@@ -1,6 +1,5 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
-import uuidByString from 'uuid-by-string';
import { ingestChallenges } from '../seed/data/challenge-ingest';
import { prisma } from '../src';
@@ -10,28 +9,30 @@ const __dirname = path.dirname(__filename);
const challengePath = path.join(__dirname, '../../../challenges');
export const slugify = (str: string) => str.toLowerCase().replace(/\s/g, '-');
-const TYPEHERO_ID = uuidByString('typehero');
try {
- const typeHeroUser = await prisma.user.findFirstOrThrow({
- where: { id: TYPEHERO_ID },
- });
-
const challengesToUpdateOrCreate = await ingestChallenges(challengePath);
const transactions = [];
for (const challenge of challengesToUpdateOrCreate) {
+ const author = await prisma.user.findFirstOrThrow({
+ where: {
+ name: challenge.author,
+ },
+ });
+ const { author: _, ...challengeWithoutAuthor } = challenge;
transactions.push(
prisma.challenge.upsert({
where: {
slug: challenge.slug,
},
update: {
- ...challenge,
+ ...challengeWithoutAuthor,
+ userId: author.id,
},
create: {
- ...challenge,
- userId: typeHeroUser.id,
+ ...challengeWithoutAuthor,
+ userId: author.id,
},
}),
);