From 1f4d39ac1a9214158c372726d107e7eb6c9b8147 Mon Sep 17 00:00:00 2001 From: Samuel Plumppu <6125097+Greenheart@users.noreply.github.com> Date: Wed, 25 Sep 2024 15:24:56 +0200 Subject: [PATCH] Find users by email instead of hard coded userIds. Also simplify seeding script --- prisma/seed.ts | 19 +------------------ scripts/import-spreadsheet-companies.ts | 4 ++-- src/routes/middlewares.ts | 14 +++++++------- 3 files changed, 10 insertions(+), 27 deletions(-) diff --git a/prisma/seed.ts b/prisma/seed.ts index 33846dbd..550b3d3d 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -22,25 +22,8 @@ async function common() { return Promise.all([seedGicsCodes(), seedUsers()]) } -async function development() { - await common() -} - -async function test() { - await common() -} - async function main() { - switch (process.env.NODE_ENV) { - case 'development': - await development() - break - case 'test': - await test() - break - default: - break - } + common() } main() diff --git a/scripts/import-spreadsheet-companies.ts b/scripts/import-spreadsheet-companies.ts index 0b58d3a9..abcab422 100644 --- a/scripts/import-spreadsheet-companies.ts +++ b/scripts/import-spreadsheet-companies.ts @@ -44,11 +44,11 @@ const ENV = envSchema.parse(process.env) const USERS = { garbo: { - id: 1, + email: 'hej@klimatkollen.se', token: ENV.API_TOKENS.garbo, }, alex: { - id: 2, + email: 'alex@klimatkollen.se', token: ENV.API_TOKENS.alex, }, } diff --git a/src/routes/middlewares.ts b/src/routes/middlewares.ts index 9e113f8f..dd8c95cb 100644 --- a/src/routes/middlewares.ts +++ b/src/routes/middlewares.ts @@ -43,8 +43,8 @@ export const cache = () => { } const USERS = { - garbo: 1, - alex: 2, + garbo: 'hej@klimatkollen.se', + alex: 'alex@klimatkollen.se', } export const fakeAuth = @@ -55,7 +55,7 @@ export const fakeAuth = if (ENV.API_TOKENS.includes(token)) { const [username] = token.split(':') const user = await prisma.user.findFirst({ - where: { id: USERS[username] }, + where: { email: USERS[username] }, }) res.locals.user = user } @@ -93,8 +93,8 @@ export const createMetadata = if (req.method === 'POST') { // TODO: Find a better way to determine if changes by the current user should count as verified or not // IDEA: Maybe a column in the User table to determine if this is a trusted editor? And if so, all their changes are automatically "verified". - const verifiedByUserId = - res.locals.user.id === USERS.alex ? USERS.alex : null + const verifiedByUserEmail = + res.locals.user.email === USERS.alex ? USERS.alex : null const { comment, source, dataOrigin } = req.body.metadata ?? {} @@ -108,10 +108,10 @@ export const createMetadata = id: res.locals.user.id, }, }, - verifiedBy: verifiedByUserId + verifiedBy: verifiedByUserEmail ? { connect: { - id: verifiedByUserId, + email: verifiedByUserEmail, }, } : undefined,