Skip to content

Commit 5f2821c

Browse files
committed
refactor: migrate user repository functions to use Prisma client
1 parent 30ef9c5 commit 5f2821c

File tree

11 files changed

+78
-95
lines changed

11 files changed

+78
-95
lines changed

.env.test

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
DATABASE_URL="postgresql://diego@localhost:5432/fullstock?schema=public"
2+
3+
# Admin Database (for database creation/deletion)
4+
ADMIN_DB_NAME=postgres
5+
6+
# This was inserted by `prisma init`:
7+
[object Promise]

playwright.config.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { defineConfig, devices } from '@playwright/test';
1+
import { defineConfig, devices } from "@playwright/test";
2+
import dotenv from "dotenv";
3+
4+
// Load test environment variables
5+
dotenv.config({ path: ".env.test" });
26

37
/**
48
* Read environment variables from file.
@@ -12,7 +16,7 @@ import { defineConfig, devices } from '@playwright/test';
1216
* See https://playwright.dev/docs/test-configuration.
1317
*/
1418
export default defineConfig({
15-
testDir: './src/e2e',
19+
testDir: "./src/e2e",
1620
/* Run tests in files in parallel */
1721
fullyParallel: true,
1822
/* Fail the build on CI if you accidentally left test.only in the source code. */
@@ -22,31 +26,31 @@ export default defineConfig({
2226
/* Opt out of parallel tests on CI. */
2327
workers: process.env.CI ? 1 : undefined,
2428
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
25-
reporter: 'html',
29+
reporter: "html",
2630
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
2731
use: {
2832
/* Base URL to use in actions like `await page.goto('/')`. */
2933
// baseURL: 'http://localhost:3000',
3034

3135
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
32-
trace: 'on-first-retry',
36+
trace: "on-first-retry",
3337
},
3438

3539
/* Configure projects for major browsers */
3640
projects: [
3741
{
38-
name: 'chromium',
39-
use: { ...devices['Desktop Chrome'] },
42+
name: "chromium",
43+
use: { ...devices["Desktop Chrome"] },
4044
},
4145

4246
{
43-
name: 'firefox',
44-
use: { ...devices['Desktop Firefox'] },
47+
name: "firefox",
48+
use: { ...devices["Desktop Firefox"] },
4549
},
4650

4751
{
48-
name: 'webkit',
49-
use: { ...devices['Desktop Safari'] },
52+
name: "webkit",
53+
use: { ...devices["Desktop Safari"] },
5054
},
5155

5256
/* Test against mobile viewports. */

src/e2e/demo.signin.spec.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import { test, expect } from "@playwright/test";
22

3+
import { prisma } from "@/db/prisma";
34
import { hashPassword } from "@/lib/security";
45
import type { CreateUserDTO } from "@/models/user.model";
5-
import {
6-
createUser,
7-
deleteUser,
8-
getUserByEmail,
9-
} from "@/repositories/user.repository";
106

117
test.describe("Visitante inicio sesion", () => {
128
let testUserId: number;
@@ -19,18 +15,26 @@ test.describe("Visitante inicio sesion", () => {
1915
isGuest: false,
2016
};
2117

22-
const existingUser = await getUserByEmail(testUser.email);
18+
const existingUser = await prisma.user.findUnique({
19+
where: { email: testUser.email },
20+
});
2321

2422
if (existingUser) {
25-
await deleteUser(existingUser.id);
23+
await prisma.user.delete({
24+
where: { id: existingUser.id },
25+
});
2626
}
2727

28-
const user = await createUser(testUser);
28+
const user = await prisma.user.create({
29+
data: testUser,
30+
});
2931
testUserId = user.id;
3032
});
3133

3234
test.afterAll(async () => {
33-
await deleteUser(testUserId);
35+
await prisma.user.delete({
36+
where: { id: testUserId },
37+
});
3438
});
3539

3640
test("test", async ({ page }) => {

src/e2e/guest-create-order.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// import { createOrderFormData } from "@/lib/utils.tests";
22
import { expect, test } from "@playwright/test";
3+
34
import { createOrderFormData } from "./utils-tests-e2e";
45

56
export type OrderFormData = Record<string, string>;

src/e2e/user-create-order.spec.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
import { test, expect } from "@playwright/test";
22

3+
import { prisma } from "@/db/prisma";
34
import { hashPassword } from "@/lib/security";
45
import type { CreateUserDTO } from "@/models/user.model";
5-
import {
6-
createUser,
7-
deleteUser,
8-
getUserByEmail,
9-
} from "@/repositories/user.repository";
106

117
test.describe("User", () => {
128
let testUserId: number;
@@ -19,18 +15,26 @@ test.describe("User", () => {
1915
isGuest: false,
2016
};
2117

22-
const existingUser = await getUserByEmail(testUser.email);
18+
const existingUser = await prisma.user.findUnique({
19+
where: { email: testUser.email },
20+
});
2321

2422
if (existingUser) {
25-
await deleteUser(existingUser.id);
23+
await prisma.user.delete({
24+
where: { id: existingUser.id },
25+
});
2626
}
2727

28-
const user = await createUser(testUser);
28+
const user = await prisma.user.create({
29+
data: testUser,
30+
});
2931
testUserId = user.id;
3032
});
3133

3234
test.afterAll(async () => {
33-
await deleteUser(testUserId);
35+
await prisma.user.delete({
36+
where: { id: testUserId },
37+
});
3438
});
3539

3640
test("User can create an order", async ({ page }) => {

src/models/user.model.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ export interface AuthResponse {
66
user: Omit<User, "password">;
77
token: string;
88
}
9+
10+
export type CreateUserDTO = Pick<
11+
User,
12+
"email" | "password" | "isGuest" | "name"
13+
>;

src/repositories/user.repository.ts

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/routes/login/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import { Link, redirect, useNavigation, useSubmit } from "react-router";
44
import { z } from "zod";
55

66
import { Button, Container, InputField, Section } from "@/components/ui";
7+
import { prisma } from "@/db/prisma";
78
import { comparePasswords } from "@/lib/security";
8-
import { getUserByEmail } from "@/repositories/user.repository";
99
import { redirectIfAuthenticated } from "@/services/auth.service";
1010
import {
1111
getRemoteCart,
@@ -31,7 +31,7 @@ export async function action({ request }: Route.ActionArgs) {
3131

3232
try {
3333
// Proceso de login nuevo
34-
const user = await getUserByEmail(email);
34+
const user = await prisma.user.findUnique({ where: { email } });
3535
if (!user) {
3636
return { error: "Correo electrónico o contraseña inválidos" };
3737
}

src/routes/root/components/auth-nav/auth-nav.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ describe("AuthNav Component", () => {
3939
4040
name: "Testino",
4141
isGuest: false,
42-
createdAt: new Date().toISOString(),
43-
updatedAt: new Date().toISOString(),
42+
createdAt: new Date(),
43+
updatedAt: new Date(),
4444
};
4545

4646
renderWithRouter(<AuthNav user={user} />);
@@ -57,8 +57,8 @@ describe("AuthNav Component", () => {
5757
5858
name: null,
5959
isGuest: false,
60-
createdAt: new Date().toISOString(),
61-
updatedAt: new Date().toISOString(),
60+
createdAt: new Date(),
61+
updatedAt: new Date(),
6262
};
6363

6464
renderWithRouter(<AuthNav user={user} />);

src/routes/signup/index.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { Link, redirect, useNavigation, useSubmit } from "react-router";
44
import { z } from "zod";
55

66
import { Button, Container, InputField, Section } from "@/components/ui";
7+
import { prisma } from "@/db/prisma";
78
import { hashPassword } from "@/lib/security";
89
import { debounceAsync } from "@/lib/utils";
910
import type { CreateUserDTO } from "@/models/user.model";
10-
import { createUser, getUserByEmail } from "@/repositories/user.repository";
1111
import { redirectIfAuthenticated } from "@/services/auth.service";
1212
import { linkCartToUser } from "@/services/cart.service";
1313
import { findEmail } from "@/services/user.client-service";
@@ -42,7 +42,9 @@ export async function action({ request }: Route.ActionArgs) {
4242
const sessionCartId = session.get("sessionCartId");
4343

4444
try {
45-
const existingUser = await getUserByEmail(email);
45+
const existingUser = await prisma.user.findUnique({
46+
where: { email: email },
47+
});
4648
if (existingUser) {
4749
return { error: "El correo electrónico ya existe" };
4850
}
@@ -56,7 +58,9 @@ export async function action({ request }: Route.ActionArgs) {
5658
name: null,
5759
};
5860

59-
const user = await createUser(newUser);
61+
const user = await prisma.user.create({
62+
data: newUser,
63+
});
6064
session.set("userId", user.id);
6165

6266
if (sessionCartId) {

0 commit comments

Comments
 (0)