-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add username to Prisma schema * Add route for registration * Attempt to fix build issue * Add types to register route * Add test for register route
- Loading branch information
Showing
6 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const generateMockRequest = (payload: object) => { | ||
return { | ||
json: async () => payload, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { prismaMock } from '@/__test__/singleton'; | ||
import { User } from '@prisma/client'; | ||
import { POST } from './route'; | ||
import { generateMockRequest } from '@/__test__/utils'; | ||
import { NextRequest } from 'next/server'; | ||
|
||
test('Register route returns intended user', async () => { | ||
const temporaryUser: User = { | ||
id: 1, | ||
username: 'test', | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
}; | ||
|
||
prismaMock.user.create.mockResolvedValue(temporaryUser); | ||
|
||
const req = generateMockRequest(temporaryUser); | ||
const res = await POST(req as NextRequest); | ||
const data = await res.json(); | ||
|
||
const expected = { | ||
...temporaryUser, | ||
updatedAt: temporaryUser.updatedAt.toISOString(), | ||
createdAt: temporaryUser.createdAt.toISOString(), | ||
}; | ||
|
||
expect(data.user).toEqual(expected); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import prisma from '@/lib/prisma'; | ||
import { User } from '@prisma/client'; | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
type PostRequest = { | ||
username: string; | ||
}; | ||
|
||
type PostResponse = { | ||
user: User; | ||
}; | ||
|
||
export const POST = async ( | ||
req: NextRequest | ||
): Promise<NextResponse<PostResponse>> => { | ||
const { username }: PostRequest = await req.json(); | ||
|
||
const user = await prisma.user.create({ | ||
data: { | ||
username, | ||
}, | ||
}); | ||
|
||
return NextResponse.json({ user }); | ||
}; |
8 changes: 8 additions & 0 deletions
8
backend/prisma/migrations/20250116232308_add_username_to_user/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
Warnings: | ||
- Added the required column `username` to the `User` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "User" ADD COLUMN "username" TEXT NOT NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters