1+ import { RegisterUserUseCase } from '@core/user' ;
2+ import { InjectQueue } from '@nestjs/bullmq' ;
13import { HttpStatus , Inject , Injectable } from '@nestjs/common' ;
24import { createId } from '@paralleldrive/cuid2' ;
35import { CACHE_SERVICE } from '@shared/adapters/cache/constants' ;
46import { ICacheService } from '@shared/adapters/cache/ports' ;
57import { BaseException } from '@shared/error' ;
8+ import { Queue } from 'bullmq' ;
69
10+ import { AuthQueues , AuthUserJobs } from '../../../domain/enums' ;
711import { OAuthErrorCodes , OAuthErrorMessages } from '../../../domain/errors' ;
8- import { ISessionRepository } from '../../../domain/repository' ;
12+ import { CreateUserWorkspaceEvent } from '../../../domain/events' ;
13+ import { IIdentityRepository , ISessionRepository } from '../../../domain/repository' ;
914import { EXCHANGE_TOKEN_NAME } from '../../../infrastructure/constants' ;
1015import { TokenService } from '../../../infrastructure/security' ;
1116import { ExchangeDto , type IOAuthExchangeData } from '../../dtos' ;
@@ -19,7 +24,12 @@ export class ExchangeUseCase {
1924 private readonly sessionRepo : ISessionRepository ,
2025 @Inject ( CACHE_SERVICE )
2126 private readonly cacheService : ICacheService ,
27+ @InjectQueue ( AuthQueues . AUTH_USER )
28+ private readonly queue : Queue ,
29+ @Inject ( 'IIdentityRepository' )
30+ private readonly identityRepo : IIdentityRepository ,
2231 private readonly tokenService : TokenService ,
32+ private readonly registerUserUC : RegisterUserUseCase ,
2333 ) { }
2434
2535 async execute ( dto : ExchangeDto , meta : DeviceMetadata ) {
@@ -40,7 +50,7 @@ export class ExchangeUseCase {
4050 const data : IOAuthExchangeData = JSON . parse ( rawData ) ;
4151 await this . cacheService . removeOne ( key ) ;
4252
43- if ( ! data . userId || ! data . email || data . provider !== dto . provider ) {
53+ if ( ! data . email || data . provider ) {
4454 await this . cacheService . removeOne ( key ) ;
4555 throw new BaseException (
4656 {
@@ -51,18 +61,47 @@ export class ExchangeUseCase {
5161 ) ;
5262 }
5363
64+ if ( data . provider !== dto . provider ) {
65+ throw new BaseException (
66+ {
67+ code : OAuthErrorCodes . EXCHANGE_TOKEN_INVALID ,
68+ message : OAuthErrorMessages [ OAuthErrorCodes . EXCHANGE_TOKEN_INVALID ] ,
69+ } ,
70+ HttpStatus . BAD_REQUEST ,
71+ ) ;
72+ }
73+
5474 try {
75+ const user = await this . registerUserUC . execute ( {
76+ email : data . email ,
77+ firstName : data . first_name || 'User' ,
78+ lastName : data . last_name ?? '' ,
79+ password : null ,
80+ bio : data . bio ,
81+ gender : data . sex === 'male' ? 'male' : data . sex === 'female' ? 'female' : 'none' ,
82+ avatarUrl : data . avatar_url ,
83+ } ) ;
84+
85+ await this . identityRepo . create ( {
86+ userId : user . id ,
87+ avatarUrl : data . avatar_url ,
88+ provider : data . provider ,
89+ providerUserId : data . id ,
90+ email : data . email ,
91+ } ) ;
92+
5593 const sessionId = createId ( ) ;
94+
5695 const { access, expiresAt, refresh } = await this . tokenService . generateTokens (
57- { id : data . userId , email : data . email } ,
96+ { id : user . id , email : user . email } ,
5897 sessionId ,
5998 ) ;
6099
61100 const result = await this . sessionRepo . create ( {
62101 id : sessionId ,
63102 ...meta ,
64103 expiresAt : expiresAt . toISOString ( ) ,
65- userId : data . userId ,
104+ userId : user . id ,
66105 } ) ;
67106
68107 if ( ! result ?. id ) {
@@ -75,11 +114,13 @@ export class ExchangeUseCase {
75114 ) ;
76115 }
77116
117+ const event = new CreateUserWorkspaceEvent ( user . id , user . firstName ) ;
118+ await this . queue . add ( AuthUserJobs . CREATE_WORKSPACE , event ) ;
119+
78120 return {
79121 success : true ,
80122 message : 'Вход выполнен успешно' ,
81123 access,
82- isNewUser : data . isNewUser ,
83124 provider : data . provider ,
84125 refresh,
85126 expiresAt,
0 commit comments