@@ -10,7 +10,7 @@ import { UpdateUserData } from '../domain/user.interface';
1010import { CreateUserData } from '../domain/user.interface' ;
1111import { PlanNotFoundException } from '@/common/exceptions/plan-not-found.exception' ;
1212import { UserNotFoundException } from '@/common/exceptions/user-not-found.exception' ;
13- import { Prisma } from 'generated/prisma/edge' ;
13+ import { Prisma , OutboxStatus } from 'generated/prisma/edge' ;
1414
1515@Injectable ( )
1616export class UserRepositoryImpl implements UserRepository {
@@ -99,38 +99,58 @@ export class UserRepositoryImpl implements UserRepository {
9999 throw new PlanNotFoundException ( ) ;
100100 }
101101
102- const user = await this . prisma . user . create ( {
103- data : {
104- name,
105- email,
106- username,
107- password,
108- ...( preferences && { preferences } ) ,
109- roles : {
110- create :
111- roles ?. map ( ( role ) => ( {
112- role : {
113- connect : { name : role } ,
114- } ,
115- } ) ) || [ ] ,
116- } ,
117- subscription : {
118- create : subscription
119- ? {
120- status : 'active' ,
121- stripeSubscriptionId : '' ,
122- currentPeriodEnd : null ,
123- plan : {
124- connect : {
125- id : plan ! . id ,
126- name : subscription ,
102+ const user = await this . prisma . $transaction ( async ( prisma ) => {
103+ const u = await this . prisma . user . create ( {
104+ data : {
105+ name,
106+ email,
107+ username,
108+ password,
109+ ...( preferences && { preferences } ) ,
110+ roles : {
111+ create :
112+ roles ?. map ( ( role ) => ( {
113+ role : {
114+ connect : { name : role } ,
127115 } ,
128- } ,
129- }
130- : undefined ,
131- } ,
132- } ,
133- } ) ;
116+ } ) ) || [ ] ,
117+ } ,
118+ subscription : {
119+ create : subscription
120+ ? {
121+ status : 'active' ,
122+ stripeSubscriptionId : '' ,
123+ currentPeriodEnd : null ,
124+ plan : {
125+ connect : {
126+ id : plan ! . id ,
127+ name : subscription ,
128+ } ,
129+ } ,
130+ }
131+ : undefined ,
132+ } ,
133+ } ,
134+ } ) ;
135+
136+ await prisma . outboxEvent . create ( {
137+ data : {
138+ topic : 'user_registered' ,
139+ payload : JSON . stringify ( {
140+ name : u . name ,
141+ to : u . email ,
142+ subject : 'Welcome to superchef!' ,
143+ body : `
144+ Thank you for registering at superchef.
145+ We are excited to have you on board!
146+ ` ,
147+ } ) ,
148+ status : OutboxStatus . PENDING ,
149+ } ,
150+ } ) ;
151+
152+ return u ;
153+ } )
134154
135155 return user ;
136156 }
0 commit comments