(userData: CreateUserData)
| 71 | export function DrizzleAdapter(db: MySql2Database): Adapter { |
| 72 | return { |
| 73 | async createUser(userData: CreateUserData) { |
| 74 | const normalizedEmail = userData.email.toLowerCase(); |
| 75 | let userId = User.UserId.make(nanoId()); |
| 76 | await db.transaction(async (tx) => { |
| 77 | const [existingUser] = await tx |
| 78 | .select() |
| 79 | .from(users) |
| 80 | .where(eq(users.email, normalizedEmail)) |
| 81 | .limit(1); |
| 82 | |
| 83 | if (existingUser) { |
| 84 | userId = existingUser.id; |
| 85 | const userUpdate: Partial<typeof users.$inferInsert> = {}; |
| 86 | |
| 87 | if (!existingUser.name && userData.name) { |
| 88 | userUpdate.name = userData.name; |
| 89 | } |
| 90 | if (!existingUser.name && !userData.name) { |
| 91 | userUpdate.name = getProvisionedUserName(normalizedEmail); |
| 92 | } |
| 93 | if (!existingUser.emailVerified && userData.emailVerified) { |
| 94 | userUpdate.emailVerified = userData.emailVerified; |
| 95 | } |
| 96 | if (!existingUser.image && userData.image) { |
| 97 | userUpdate.image = userData.image as ImageUpload.ImageUrlOrKey; |
| 98 | } |
| 99 | |
| 100 | if (Object.keys(userUpdate).length > 0) { |
| 101 | await tx.update(users).set(userUpdate).where(eq(users.id, userId)); |
| 102 | } |
| 103 | |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | const [pendingInvite] = await tx |
| 108 | .select({ id: organizationInvites.id }) |
| 109 | .from(organizationInvites) |
| 110 | .where( |
| 111 | and( |
| 112 | eq(organizationInvites.invitedEmail, normalizedEmail), |
| 113 | eq(organizationInvites.status, "pending"), |
| 114 | ), |
| 115 | ) |
| 116 | .limit(1); |
| 117 | |
| 118 | await tx.insert(users).values({ |
| 119 | id: userId, |
| 120 | email: normalizedEmail, |
| 121 | emailVerified: userData.emailVerified, |
| 122 | name: userData.name, |
| 123 | image: userData.image as ImageUpload.ImageUrlOrKey | null, |
| 124 | activeOrganizationId: Organisation.OrganisationId.make(""), |
| 125 | }); |
| 126 | |
| 127 | if (pendingInvite) { |
| 128 | return; |
| 129 | } |
| 130 |
no test coverage detected