Skip to content

Commit

Permalink
ユーザーモデルからemailフィールドを削除し、userEmailモデルを追加して関連するテストを更新しました。 (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
ttizze authored Jan 19, 2025
2 parents 0137a1b + 78f9179 commit c7d41b7
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 91 deletions.
13 changes: 8 additions & 5 deletions web/app/features/translate/functions/mutations.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ import { prisma } from "~/utils/prisma";

export async function getOrCreateAIUser(name: string): Promise<number> {
const user = await prisma.user.upsert({
where: { email: `${name}@ai.com` },
where: { userName: name },
update: {},
create: {
email: `${name}@ai.com`,
isAI: true,
icon: "",
userName: name,
displayName: name,
isAI: true,
icon: "",
userEmail: {
create: {
email: `${name}@ai.com`,
},
},
},
});

return user.id;
}

Expand Down
1 change: 0 additions & 1 deletion web/app/features/translate/lib/translate.server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe("translate関数テスト (geminiのみモック)", () => {
const user = await prisma.user.create({
data: {
userName: "testuser",
email: "[email protected]",
displayName: "testuser",
icon: "testuser",
},
Expand Down
2 changes: 0 additions & 2 deletions web/app/routes/$locale+/user.$userName+/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ describe("UserProfile", () => {
data: {
userName: "testuser",
displayName: "Test User",
email: "[email protected]",
icon: "https://example.com/icon.jpg",
profile: "This is a test profile",
pages: {
Expand Down Expand Up @@ -71,7 +70,6 @@ describe("UserProfile", () => {
data: {
userName: "testuser2",
displayName: "Test User2",
email: "[email protected]",
icon: "https://example.com/icon2.jpg",
profile: "This is a test profile2",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ describe("EditPage", () => {
data: {
userName: "testuser",
displayName: "Test User",
email: "[email protected]",
icon: "https://example.com/icon.jpg",
profile: "This is a test profile",
pages: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ describe("processHtmlContent", () => {
create: {
id: 10,
userName: "htmltester",
email: "[email protected]",
displayName: "htmltester",
icon: "htmltester",
},
Expand Down Expand Up @@ -86,7 +85,6 @@ describe("processHtmlContent", () => {
create: {
id: 11,
userName: "htmleditor",
email: "[email protected]",
displayName: "htmleditor",
icon: "htmleditor",
},
Expand Down Expand Up @@ -182,7 +180,6 @@ describe("processHtmlContent", () => {
create: {
id: 12,
userName: "titleduplicateuser",
email: "[email protected]",
displayName: "titleduplicateuser",
icon: "titleduplicateuser",
},
Expand Down Expand Up @@ -268,7 +265,6 @@ describe("processHtmlContent", () => {
create: {
id: 13,
userName: "noedit",
email: "[email protected]",
displayName: "noedit",
icon: "noedit",
},
Expand Down Expand Up @@ -345,7 +341,6 @@ describe("processHtmlContent", () => {
create: {
id: 14,
userName: "imagetester",
email: "[email protected]",
displayName: "imagetester",
icon: "imagetester",
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ describe("toggleLike 実際のDB統合テスト", () => {
data: {
userName: "testuser",
displayName: "Test User",
email: "[email protected]",
icon: "https://example.com/icon.jpg",
profile: "This is a test profile",
pages: {
Expand Down
25 changes: 19 additions & 6 deletions web/app/utils/auth.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ const formStrategy = new FormStrategy(async ({ form }) => {
throw new AuthorizationError("Email and password are required");
}

const existingUser = await prisma.user.findUnique({
const existingUserEmail = await prisma.userEmail.findUnique({
where: { email: String(email) },
include: { credential: true },
include: { user: { include: { credential: true } } },
});
const existingUser = existingUserEmail?.user;
if (
!existingUser ||
!existingUser.credential?.password ||
Expand Down Expand Up @@ -62,16 +63,22 @@ const googleStrategy = new GoogleStrategy<User>(
callbackURL: `${process.env.CLIENT_URL}/api/auth/callback/google`,
},
async ({ profile }) => {
const user = await prisma.user.findUnique({
const userEmail = await prisma.userEmail.findUnique({
where: { email: profile.emails[0].value },
include: { user: true },
});
const user = userEmail?.user;
if (user) {
return user;
}

const newUser = await prisma.user.create({
data: {
email: profile.emails[0].value || "",
userEmail: {
create: {
email: profile.emails[0].value || "",
},
},
userName: generateTemporaryUserName(),
displayName: profile.displayName || "New User",
icon: profile.photos[0].value || "",
Expand All @@ -91,17 +98,23 @@ const magicLinkStrategy = new EmailLinkStrategy(
sessionMagicLinkKey: "auth:magicLink",
},
async ({ email, form, magicLinkVerify }) => {
const user = await prisma.user.findUnique({
const userEmail = await prisma.userEmail.findUnique({
where: { email: String(email) },
include: { user: true },
});
const user = userEmail?.user;

if (user) {
return user;
}

const newUser = await prisma.user.create({
data: {
email: String(email),
userEmail: {
create: {
email: String(email),
},
},
icon: `${process.env.CLIENT_URL}/avatar.png`,
userName: generateTemporaryUserName(),
displayName: String(email).split("@")[0],
Expand Down
42 changes: 42 additions & 0 deletions web/prisma/migrations/20250119135311_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Warnings:
- You are about to drop the column `email` on the `users` table. All the data in the column will be lost.
*/


CREATE TABLE "user_emails" (
"id" SERIAL NOT NULL,
"email" TEXT NOT NULL,
"user_id" INTEGER NOT NULL,

CONSTRAINT "user_emails_pkey" PRIMARY KEY ("id")
);

INSERT INTO "user_emails" ("email", "user_id")
SELECT "email", "id"
FROM "users"
WHERE "email" IS NOT NULL;

-- DropIndex
DROP INDEX "users_email_idx";

-- DropIndex
DROP INDEX "users_email_key";

-- AlterTable
ALTER TABLE "users" DROP COLUMN "email";

-- CreateTable
-- CreateIndex
CREATE UNIQUE INDEX "user_emails_email_key" ON "user_emails"("email");

-- CreateIndex
CREATE UNIQUE INDEX "user_emails_user_id_key" ON "user_emails"("user_id");

-- CreateIndex
CREATE INDEX "user_emails_user_id_idx" ON "user_emails"("user_id");

-- AddForeignKey
ALTER TABLE "user_emails" ADD CONSTRAINT "user_emails_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
Loading

0 comments on commit c7d41b7

Please sign in to comment.