-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
288 additions
and
41 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,29 @@ | ||
import { PROVIDER_LIST } from 'common/constants'; | ||
import { userQuery } from 'domain/user/repository/userQuery'; | ||
import { socialUseCase } from 'domain/user/useCase/socialUseCase'; | ||
import { brandedId } from 'service/brandedId'; | ||
import { prismaClient } from 'service/prismaClient'; | ||
import { z } from 'zod'; | ||
import { defineController } from './$relay'; | ||
|
||
export default defineController(() => ({ | ||
get: { | ||
validators: { query: z.object({ userPoolClientId: brandedId.userPoolClient.maybe }) }, | ||
handler: async ({ query }) => ({ | ||
status: 200, | ||
body: await userQuery.listSocials(prismaClient, query.userPoolClientId), | ||
}), | ||
}, | ||
post: { | ||
validators: { | ||
body: z.object({ | ||
provider: z.enum(PROVIDER_LIST), | ||
name: z.string(), | ||
email: z.string(), | ||
photoUrl: z.string().optional(), | ||
userPoolClientId: brandedId.userPoolClient.maybe, | ||
}), | ||
}, | ||
handler: async ({ body }) => ({ status: 200, body: await socialUseCase.createUser(body) }), | ||
}, | ||
})); |
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,14 @@ | ||
import type { DefineMethods } from 'aspida'; | ||
import type { MaybeId } from 'common/types/brandedId'; | ||
import type { SocialUserCreateVal, SocialUserEntity } from 'common/types/user'; | ||
|
||
export type Methods = DefineMethods<{ | ||
get: { | ||
query: { userPoolClientId: MaybeId['userPoolClient'] }; | ||
resBody: SocialUserEntity[]; | ||
}; | ||
post: { | ||
reqBody: SocialUserCreateVal; | ||
resBody: SocialUserEntity; | ||
}; | ||
}>; |
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
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
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,38 @@ | ||
import assert from 'assert'; | ||
import type { EntityId } from 'common/types/brandedId'; | ||
import type { SocialUserCreateVal, SocialUserEntity } from 'common/types/user'; | ||
import { brandedId } from 'service/brandedId'; | ||
import { cognitoAssert } from 'service/cognitoAssert'; | ||
import { ulid } from 'ulid'; | ||
import { z } from 'zod'; | ||
import { createAttributes } from '../service/createAttributes'; | ||
|
||
export const socialUserMethod = { | ||
create: (userPoolId: EntityId['userPool'], val: SocialUserCreateVal): SocialUserEntity => { | ||
cognitoAssert( | ||
/^[a-z][a-z\d_-]/.test(val.name), | ||
"1 validation error detected: Value at 'username' failed to satisfy constraint: Member must satisfy regular expression pattern: [\\p{L}\\p{M}\\p{S}\\p{N}\\p{P}]+", | ||
); | ||
cognitoAssert(z.string().email().parse(val.email), 'Invalid email address format.'); | ||
assert(z.string().url().optional().safeParse(val.photoUrl).success, 'Invalid photoUrl format.'); | ||
|
||
const now = Date.now(); | ||
|
||
return { | ||
id: brandedId.socialUser.entity.parse(ulid()), | ||
kind: 'social', | ||
email: val.email, | ||
provider: val.provider, | ||
enabled: true, | ||
status: 'EXTERNAL_PROVIDER', | ||
name: val.name, | ||
refreshToken: ulid(), | ||
userPoolId, | ||
attributes: val.photoUrl | ||
? createAttributes([{ Name: 'picture', Value: val.photoUrl }], []) | ||
: [], | ||
createdTime: now, | ||
updatedTime: now, | ||
}; | ||
}, | ||
}; |
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
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 |
---|---|---|
@@ -1,27 +1,45 @@ | ||
import type { Prisma } from '@prisma/client'; | ||
import type { EntityId } from 'common/types/brandedId'; | ||
import type { CognitoUserEntity } from 'common/types/user'; | ||
import { toUserEntity } from './toUserEntity'; | ||
import { USER_KINDS } from 'common/constants'; | ||
import type { EntityId, MaybeId } from 'common/types/brandedId'; | ||
import type { CognitoUserEntity, SocialUserEntity } from 'common/types/user'; | ||
import { toCognitoUserEntity, toSocialUserEntity } from './toUserEntity'; | ||
|
||
export const userQuery = { | ||
countId: (tx: Prisma.TransactionClient, id: string): Promise<number> => | ||
tx.user.count({ where: { id } }), | ||
listByPoolId: (tx: Prisma.TransactionClient, userPoolId: string): Promise<CognitoUserEntity[]> => | ||
listSocials: ( | ||
tx: Prisma.TransactionClient, | ||
userPoolClientId: MaybeId['userPoolClient'], | ||
): Promise<SocialUserEntity[]> => | ||
tx.user | ||
.findMany({ | ||
where: { | ||
UserPool: { userPoolClients: { some: { id: userPoolClientId } } }, | ||
kind: USER_KINDS.social, | ||
}, | ||
include: { attributes: true }, | ||
}) | ||
.then((users) => users.map(toSocialUserEntity)), | ||
listCognitos: (tx: Prisma.TransactionClient, userPoolId: string): Promise<CognitoUserEntity[]> => | ||
tx.user | ||
.findMany({ where: { userPoolId }, include: { attributes: true } }) | ||
.then((users) => users.map(toUserEntity)), | ||
.findMany({ where: { userPoolId, kind: USER_KINDS.cognito }, include: { attributes: true } }) | ||
.then((users) => users.map(toCognitoUserEntity)), | ||
findById: ( | ||
tx: Prisma.TransactionClient, | ||
id: EntityId['cognitoUser'], | ||
): Promise<CognitoUserEntity> => | ||
tx.user.findUniqueOrThrow({ where: { id }, include: { attributes: true } }).then(toUserEntity), | ||
tx.user | ||
.findUniqueOrThrow({ where: { id }, include: { attributes: true } }) | ||
.then(toCognitoUserEntity), | ||
findByName: (tx: Prisma.TransactionClient, name: string): Promise<CognitoUserEntity> => | ||
tx.user.findFirstOrThrow({ where: { name }, include: { attributes: true } }).then(toUserEntity), | ||
tx.user | ||
.findFirstOrThrow({ where: { name }, include: { attributes: true } }) | ||
.then(toCognitoUserEntity), | ||
findByRefreshToken: ( | ||
tx: Prisma.TransactionClient, | ||
refreshToken: string, | ||
): Promise<CognitoUserEntity> => | ||
tx.user | ||
.findFirstOrThrow({ where: { refreshToken }, include: { attributes: true } }) | ||
.then(toUserEntity), | ||
.then(toCognitoUserEntity), | ||
}; |
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
Oops, something went wrong.