Skip to content

Commit

Permalink
Merge pull request #175 from Organisasjonskollegiet/migrate-database
Browse files Browse the repository at this point in the history
Remove saveAuthUser to fix bug
  • Loading branch information
Arashfa0301 authored Oct 23, 2024
2 parents 16f3060 + 85fcf14 commit 89da3e6
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 289 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## this is the stage one , also know as the build step

FROM node:14-alpine
FROM node:14
WORKDIR /app
COPY package.json ./
COPY yarn.lock ./
Expand All @@ -13,7 +13,7 @@ RUN yarn run build

## this is stage two , where the app actually runs

FROM node:14-alpine
FROM node:14

WORKDIR /app
COPY package.json ./
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"apollo-server-express": "2.25.3",
"auth0": "^2.40.0",
"axios": "^0.26.0",
"bcrypt": "^5.0.1",
"bcryptjs": "^2.4.3",
"casual": "^1.6.2",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
Expand All @@ -48,7 +48,7 @@
},
"devDependencies": {
"@types/auth0": "^2.34.13",
"@types/bcrypt": "^5.0.0",
"@types/bcryptjs": "2.4.6",
"@types/cookie-parser": "^1.4.2",
"@types/express-jwt": "^6.0.4",
"@types/graphql-iso-date": "^3.4.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Warnings:
- The primary key for the `HasVoted` table will be changed. If it partially fails, the table could be left without primary key constraint.
- The primary key for the `User` table will be changed. If it partially fails, the table could be left without primary key constraint.
*/
-- DropForeignKey
ALTER TABLE "HasVoted" DROP CONSTRAINT "HasVoted_userId_fkey";

-- DropForeignKey
ALTER TABLE "Meeting" DROP CONSTRAINT "Meeting_ownerId_fkey";

-- DropForeignKey
ALTER TABLE "Participant" DROP CONSTRAINT "Participant_userId_fkey";

-- AlterTable
ALTER TABLE "HasVoted" DROP CONSTRAINT "HasVoted_pkey",
ALTER COLUMN "userId" SET DATA TYPE VARCHAR(255),
ADD CONSTRAINT "HasVoted_pkey" PRIMARY KEY ("userId", "votationId");

-- AlterTable
ALTER TABLE "Meeting" ALTER COLUMN "ownerId" SET DATA TYPE VARCHAR(255);

-- AlterTable
ALTER TABLE "Participant" ALTER COLUMN "userId" SET DATA TYPE VARCHAR(255);

-- AlterTable
ALTER TABLE "User" DROP CONSTRAINT "User_pkey",
ALTER COLUMN "id" SET DATA TYPE VARCHAR(255),
ADD CONSTRAINT "User_pkey" PRIMARY KEY ("id");

-- AddForeignKey
ALTER TABLE "Meeting" ADD CONSTRAINT "Meeting_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "Participant" ADD CONSTRAINT "Participant_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "HasVoted" ADD CONSTRAINT "HasVoted_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
8 changes: 4 additions & 4 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ enum VotationStatus {
}

model User {
id String @id @default(uuid()) @db.Uuid
id String @id @db.VarChar(255)
email String @unique @db.VarChar(255)
emailVerified Boolean @default(false)
password String
Expand All @@ -53,7 +53,7 @@ model Meeting {
startTime DateTime
description String?
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
ownerId String @db.Uuid
ownerId String @db.VarChar(255)
votations Votation[]
status MeetingStatus @default(UPCOMING)
allowSelfRegistration Boolean @default(false)
Expand All @@ -64,7 +64,7 @@ model Meeting {
model Participant {
id String @id @default(uuid()) @db.Uuid
role Role
userId String @db.Uuid
userId String @db.VarChar(255)
meetingId String @db.Uuid
isVotingEligible Boolean @default(true)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
Expand Down Expand Up @@ -118,7 +118,7 @@ model Votation {

model HasVoted {
votationId String @db.Uuid
userId String @db.Uuid
userId String @db.VarChar(255)
createdAt DateTime @default(now())
votation Votation @relation(fields: [votationId], references: [id], onDelete: Cascade)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
Expand Down
1 change: 1 addition & 0 deletions src/lib/tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export const createAlternative = async (
export const createUser = async (ctx: TestContext) => {
return await ctx.prisma.user.create({
data: {
id: '1',
email: casual.email,
password: casual.password,
},
Expand Down
2 changes: 2 additions & 0 deletions src/schema/votation/__tests__/votation.castVote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createMeeting, createVotation, createAlternative } from '../../../lib/t
import { createTestContext } from '../../../lib/tests/testContext';
import { VotationStatus, Role } from '.prisma/client';
import { gql } from 'graphql-request';

const ctx = createTestContext();

const alternativeText = 'alt';
Expand Down Expand Up @@ -70,6 +71,7 @@ it('should not cast vote successfully since votation is not ongoing', async () =
it('should not cast vote successfully since user is not participant', async () => {
const meetingOwner = await ctx.prisma.user.create({
data: {
id: '234', // Generate a new UUID for the test user
email: '[email protected]',
password: 'password',
},
Expand Down
8 changes: 4 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { protectedSchema } from './schema';
import 'dotenv/config';
import { Context } from './context';
import { checkJwt, DecodedToken } from './lib/auth/verifyToken';
import { saveAuth0UserIfNotExist } from './utils/save_user_locally';
// import { saveAuth0UserIfNotExist } from './utils/save_user_locally';
import cors from 'cors';

export const createApollo = (prisma: PrismaClient) => {
Expand All @@ -17,9 +17,9 @@ export const createApollo = (prisma: PrismaClient) => {
if (req.user) {
const decodedToken = req.user as DecodedToken;
const userId = decodedToken.sub.split('|')[1];
if (process.env.NODE_ENV != 'production') {
await saveAuth0UserIfNotExist(prisma, userId, req.headers['authorization']);
}
// if (process.env.NODE_ENV != 'production') {
// await saveAuth0UserIfNotExist(prisma, userId, req.headers['authorization']);
// }
return { userId: userId, prisma };
}
return { userId: '', prisma };
Expand Down
6 changes: 3 additions & 3 deletions src/utils/populatedb.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PrismaClient } from '@prisma/client';
import 'dotenv/config';
import casual from 'casual';
import bcrypt from 'bcrypt';
import bcryptjs from 'bcryptjs';

export const populatedb = async () => {
const prisma = new PrismaClient();
Expand All @@ -11,9 +11,9 @@ export const populatedb = async () => {
const users = await Promise.all(
[...new Array(5)].map(async () => {
// Replicates a hashed password
const password = await bcrypt.hash(casual.password, 3);
const password = await bcryptjs.hash(casual.password, 3);
const user = await prisma.user.create({
data: { email: casual.email, password: password },
data: { id: casual.uuid, email: casual.email, password: password },
});
return user;
})
Expand Down
30 changes: 15 additions & 15 deletions src/utils/save_user_locally.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ interface Auth0Profile {

// Saves the auth0 user profile to the database if it does not exist locally
// This is mainly to make local development easier
export const saveAuth0UserIfNotExist = async (prisma: PrismaClient, userId: string, authHeader?: string) => {
const userCount = await prisma.user.count({ where: { id: userId } });
if (userCount == 0 && authHeader) {
const request = await axios.get<Auth0Profile>(`https://${process.env.AUTH0_DOMAIN}/userinfo`, {
headers: {
Authorization: authHeader,
'Content-Type': 'application/json',
},
});
const profile = request.data;
await prisma.user.create({
data: { email: profile.email, password: '', id: userId, emailVerified: false },
});
}
};
// export const saveAuth0UserIfNotExist = async (prisma: PrismaClient, userId: string, authHeader?: string) => {
// const userCount = await prisma.user.count({ where: { id: userId } });
// if (userCount == 0 && authHeader) {
// const request = await axios.get<Auth0Profile>(`https://${process.env.AUTH0_DOMAIN}/userinfo`, {
// headers: {
// Authorization: authHeader,
// 'Content-Type': 'application/json',
// },
// });
// const profile = request.data;
// await prisma.user.create({
// data: { email: profile.email, password: '', id: userId, emailVerified: false },
// });
// }
// };
Loading

0 comments on commit 89da3e6

Please sign in to comment.