Skip to content

Commit c7d41b7

Browse files
authored
ユーザーモデルからemailフィールドを削除し、userEmailモデルを追加して関連するテストを更新しました。 (#526)
2 parents 0137a1b + 78f9179 commit c7d41b7

File tree

11 files changed

+159
-91
lines changed

11 files changed

+159
-91
lines changed

web/app/features/translate/functions/mutations.server.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@ import { prisma } from "~/utils/prisma";
22

33
export async function getOrCreateAIUser(name: string): Promise<number> {
44
const user = await prisma.user.upsert({
5-
where: { email: `${name}@ai.com` },
5+
where: { userName: name },
66
update: {},
77
create: {
8-
email: `${name}@ai.com`,
9-
isAI: true,
10-
icon: "",
118
userName: name,
129
displayName: name,
10+
isAI: true,
11+
icon: "",
12+
userEmail: {
13+
create: {
14+
email: `${name}@ai.com`,
15+
},
16+
},
1317
},
1418
});
15-
1619
return user.id;
1720
}
1821

web/app/features/translate/lib/translate.server.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ describe("translate関数テスト (geminiのみモック)", () => {
2424
const user = await prisma.user.create({
2525
data: {
2626
userName: "testuser",
27-
2827
displayName: "testuser",
2928
icon: "testuser",
3029
},

web/app/routes/$locale+/user.$userName+/index.test.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ describe("UserProfile", () => {
2121
data: {
2222
userName: "testuser",
2323
displayName: "Test User",
24-
2524
icon: "https://example.com/icon.jpg",
2625
profile: "This is a test profile",
2726
pages: {
@@ -71,7 +70,6 @@ describe("UserProfile", () => {
7170
data: {
7271
userName: "testuser2",
7372
displayName: "Test User2",
74-
7573
icon: "https://example.com/icon2.jpg",
7674
profile: "This is a test profile2",
7775
},

web/app/routes/$locale+/user.$userName+/page+/$slug+/edit/_edit.test.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ describe("EditPage", () => {
5555
data: {
5656
userName: "testuser",
5757
displayName: "Test User",
58-
5958
icon: "https://example.com/icon.jpg",
6059
profile: "This is a test profile",
6160
pages: {

web/app/routes/$locale+/user.$userName+/page+/$slug+/edit/utils/processHtmlContent.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ describe("processHtmlContent", () => {
1616
create: {
1717
id: 10,
1818
userName: "htmltester",
19-
2019
displayName: "htmltester",
2120
icon: "htmltester",
2221
},
@@ -86,7 +85,6 @@ describe("processHtmlContent", () => {
8685
create: {
8786
id: 11,
8887
userName: "htmleditor",
89-
9088
displayName: "htmleditor",
9189
icon: "htmleditor",
9290
},
@@ -182,7 +180,6 @@ describe("processHtmlContent", () => {
182180
create: {
183181
id: 12,
184182
userName: "titleduplicateuser",
185-
186183
displayName: "titleduplicateuser",
187184
icon: "titleduplicateuser",
188185
},
@@ -268,7 +265,6 @@ describe("processHtmlContent", () => {
268265
create: {
269266
id: 13,
270267
userName: "noedit",
271-
272268
displayName: "noedit",
273269
icon: "noedit",
274270
},
@@ -345,7 +341,6 @@ describe("processHtmlContent", () => {
345341
create: {
346342
id: 14,
347343
userName: "imagetester",
348-
349344
displayName: "imagetester",
350345
icon: "imagetester",
351346
},

web/app/routes/resources+/functions/mutations.server.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ describe("toggleLike 実際のDB統合テスト", () => {
1212
data: {
1313
userName: "testuser",
1414
displayName: "Test User",
15-
1615
icon: "https://example.com/icon.jpg",
1716
profile: "This is a test profile",
1817
pages: {

web/app/utils/auth.server.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ const formStrategy = new FormStrategy(async ({ form }) => {
3131
throw new AuthorizationError("Email and password are required");
3232
}
3333

34-
const existingUser = await prisma.user.findUnique({
34+
const existingUserEmail = await prisma.userEmail.findUnique({
3535
where: { email: String(email) },
36-
include: { credential: true },
36+
include: { user: { include: { credential: true } } },
3737
});
38+
const existingUser = existingUserEmail?.user;
3839
if (
3940
!existingUser ||
4041
!existingUser.credential?.password ||
@@ -62,16 +63,22 @@ const googleStrategy = new GoogleStrategy<User>(
6263
callbackURL: `${process.env.CLIENT_URL}/api/auth/callback/google`,
6364
},
6465
async ({ profile }) => {
65-
const user = await prisma.user.findUnique({
66+
const userEmail = await prisma.userEmail.findUnique({
6667
where: { email: profile.emails[0].value },
68+
include: { user: true },
6769
});
70+
const user = userEmail?.user;
6871
if (user) {
6972
return user;
7073
}
7174

7275
const newUser = await prisma.user.create({
7376
data: {
74-
email: profile.emails[0].value || "",
77+
userEmail: {
78+
create: {
79+
email: profile.emails[0].value || "",
80+
},
81+
},
7582
userName: generateTemporaryUserName(),
7683
displayName: profile.displayName || "New User",
7784
icon: profile.photos[0].value || "",
@@ -91,17 +98,23 @@ const magicLinkStrategy = new EmailLinkStrategy(
9198
sessionMagicLinkKey: "auth:magicLink",
9299
},
93100
async ({ email, form, magicLinkVerify }) => {
94-
const user = await prisma.user.findUnique({
101+
const userEmail = await prisma.userEmail.findUnique({
95102
where: { email: String(email) },
103+
include: { user: true },
96104
});
105+
const user = userEmail?.user;
97106

98107
if (user) {
99108
return user;
100109
}
101110

102111
const newUser = await prisma.user.create({
103112
data: {
104-
email: String(email),
113+
userEmail: {
114+
create: {
115+
email: String(email),
116+
},
117+
},
105118
icon: `${process.env.CLIENT_URL}/avatar.png`,
106119
userName: generateTemporaryUserName(),
107120
displayName: String(email).split("@")[0],
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
Warnings:
3+
4+
- You are about to drop the column `email` on the `users` table. All the data in the column will be lost.
5+
6+
*/
7+
8+
9+
CREATE TABLE "user_emails" (
10+
"id" SERIAL NOT NULL,
11+
"email" TEXT NOT NULL,
12+
"user_id" INTEGER NOT NULL,
13+
14+
CONSTRAINT "user_emails_pkey" PRIMARY KEY ("id")
15+
);
16+
17+
INSERT INTO "user_emails" ("email", "user_id")
18+
SELECT "email", "id"
19+
FROM "users"
20+
WHERE "email" IS NOT NULL;
21+
22+
-- DropIndex
23+
DROP INDEX "users_email_idx";
24+
25+
-- DropIndex
26+
DROP INDEX "users_email_key";
27+
28+
-- AlterTable
29+
ALTER TABLE "users" DROP COLUMN "email";
30+
31+
-- CreateTable
32+
-- CreateIndex
33+
CREATE UNIQUE INDEX "user_emails_email_key" ON "user_emails"("email");
34+
35+
-- CreateIndex
36+
CREATE UNIQUE INDEX "user_emails_user_id_key" ON "user_emails"("user_id");
37+
38+
-- CreateIndex
39+
CREATE INDEX "user_emails_user_id_idx" ON "user_emails"("user_id");
40+
41+
-- AddForeignKey
42+
ALTER TABLE "user_emails" ADD CONSTRAINT "user_emails_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

0 commit comments

Comments
 (0)