Skip to content

Commit

Permalink
update: db schema
Browse files Browse the repository at this point in the history
  • Loading branch information
SIY1121 committed Mar 31, 2021
1 parent c6c37ea commit 92b4402
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
11 changes: 7 additions & 4 deletions __tests__/grpc/user.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { startGrpcServer, stopGrpcServer } from '../../src/grpc'
import * as protoLoader from '@grpc/proto-loader'
import path from 'path'
import * as grpc from '@grpc/grpc-js'
import { UserService } from '../../generated'
import { Provider, UserService } from '../../generated'
import { ServiceClientConstructor } from '@grpc/grpc-js/build/src/make-client'
import { GrpcClient } from '../../src/grpc/type'
import { Status } from '@grpc/grpc-js/build/src/constants'
import { getOrCreateUserUseCase } from '../../src/usecase/getOrCreateUser'
import { mocked } from 'ts-jest/utils'
import { v4 } from 'uuid'
import { addAuthenticationUseCase } from '../../src/usecase/addAuthentication'
import { Provider } from '../../src/database/model/userAuthentications'
import { AlreadyExistError, NotFoundError } from '../../src/error'
import { getUserUseCase } from '../../src/usecase/getUser'
import { deepContaining } from '../_deepContaining'
Expand Down Expand Up @@ -68,7 +67,7 @@ describe('addAuthentication', () => {
mocked(addAuthenticationUseCase).mockImplementation(
async (id, provider, socialId) => {
expect(id).toEqual(data.id)
expect(provider).toEqual(data.provider)
expect(provider).toEqual('Google') // 後で綺麗にする
expect(socialId).toEqual(data.socialId)
}
)
Expand Down Expand Up @@ -108,11 +107,15 @@ describe('getUser', () => {
],
}
test('success', (done) => {
// 後で綺麗にする
// @ts-ignore
mocked(getUserUseCase).mockImplementation(async (id) => {
expect(id).toEqual(data.id)
return {
id,
authentications: data.authentications,
authentications: [
{ provider: 'Google', socialId: '100000000000000000' },
],
}
})
client.getUser(data, (err, res) => {
Expand Down
15 changes: 11 additions & 4 deletions src/database/model/userAuthentications.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm'
import {
Column,
Entity,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn,
} from 'typeorm'
import { User } from './user'

export enum Provider {
Google,
Twitter,
Apple,
Google = 'Google',
Twitter = 'Twitter',
Apple = 'Apple',
}

@Entity({
Expand All @@ -18,6 +24,7 @@ export class UserAuthentication {
id!: number

@ManyToOne((type) => User, (user) => user.id)
@JoinColumn({ name: 'user_id' })
user!: User

@Column({
Expand Down
33 changes: 30 additions & 3 deletions src/grpc/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import {
AddAuthenticationResponse,
GetOrCreateUserResponse,
GetUserResponse,
Provider as GProvider,
UserService,
} from '../../generated'
import { Provider } from '../database/model/userAuthentications'
import { addAuthenticationUseCase } from '../usecase/addAuthentication'
import { getOrCreateUserUseCase } from '../usecase/getOrCreateUser'
import { getUserUseCase } from '../usecase/getUser'
Expand All @@ -14,7 +16,7 @@ export const userService: GrpcServer<UserService> = {
async getOrCreateUser({ request }, callback) {
try {
const res = await getOrCreateUserUseCase(
request.provider,
toDBProvider(request.provider),
request.socialId
)
callback(null, GetOrCreateUserResponse.create({ ...res }))
Expand All @@ -26,7 +28,7 @@ export const userService: GrpcServer<UserService> = {
try {
await addAuthenticationUseCase(
request.id,
request.provider,
toDBProvider(request.provider),
request.socialId
)
callback(null, AddAuthenticationResponse.create())
Expand All @@ -41,11 +43,36 @@ export const userService: GrpcServer<UserService> = {
null,
GetUserResponse.create({
id: request.id,
authentications: user.authentications,
authentications: user.authentications.map((a) => ({
provider: toGrpcProvider(a.provider),
socialId: a.socialId,
})),
})
)
} catch (e) {
callback(toGrpcError(e))
}
},
}

function toDBProvider(provider: GProvider) {
switch (provider) {
case GProvider.Google:
return Provider.Google
case GProvider.Twitter:
return Provider.Twitter
case GProvider.Apple:
return Provider.Apple
}
}

function toGrpcProvider(provider: Provider) {
switch (provider) {
case Provider.Google:
return GProvider.Google
case Provider.Twitter:
return GProvider.Twitter
case Provider.Apple:
return GProvider.Apple
}
}

0 comments on commit 92b4402

Please sign in to comment.