@@ -47,59 +47,63 @@ export async function addUserQuizPoints(
4747}
4848
4949export async function findOrCreateUser ( msg : Message ) : Promise < boolean > {
50- try {
51- const lid = ( msg . author ?? msg . from ) . split ( "@" ) [ 0 ] ;
50+ const lid = ( msg . author ?? msg . from ) . split ( "@" ) [ 0 ] ;
5251
53- const min = 0.1 ;
54- const max = 0.9 ;
55- const rand = Math . random ( ) * ( max - min ) + min ;
56- const points = parseFloat ( rand . toFixed ( 1 ) ) ;
52+ const min = 0.1 ;
53+ const max = 0.9 ;
54+ const rand = Math . random ( ) * ( max - min ) + min ;
55+ const points = parseFloat ( rand . toFixed ( 1 ) ) ;
5756
58- const user = await prisma . user
59- . update ( {
60- where : { lid } ,
61- data : {
62- commandCount : { increment : 1 } ,
63- points : {
64- increment : points ,
57+ try {
58+ const result = await prisma . $transaction ( async ( tx ) => {
59+ const user = await tx . user . findUnique ( { where : { lid } } ) ;
60+
61+ if ( user ) {
62+ await tx . user . update ( {
63+ where : { lid } ,
64+ data : {
65+ commandCount : { increment : 1 } ,
66+ points : { increment : points } ,
6567 } ,
66- } ,
67- } )
68- . catch ( ( ) => null ) ;
68+ } ) ;
69+
70+ return false ;
71+ }
6972
70- if ( user ) return false ;
73+ const contact = await msg . getContact ( ) ;
74+ const name = contact ?. pushname || contact ?. name || "null" ;
75+ const number = contact ?. number || "0" ;
76+ const countryCode = contact
77+ ? await contact . getCountryCode ( ) . catch ( ( ) => "null" )
78+ : "null" ;
79+ const about = contact ? await contact . getAbout ( ) . catch ( ( ) => null ) : null ;
7180
72- const contact = await msg . getContact ( ) ;
81+ await Promise . allSettled ( [
82+ msg . react ( "✅" ) ,
83+ tx . user . create ( {
84+ data : {
85+ lid,
86+ name,
87+ number,
88+ countryCode,
89+ type : contact ?. isBusiness ? "business" : "private" ,
90+ mode : msg . author ? "group" : "private" ,
91+ about : about ? filterContent ( about ) : null ,
92+ commandCount : 1 ,
93+ points,
94+ } ,
95+ } ) ,
96+ ] ) ;
7397
74- const name = contact ?. pushname || contact ?. name || "null" ;
75- const number = contact ?. number || "0" ;
76- const countryCode = contact
77- ? await contact . getCountryCode ( ) . catch ( ( ) => "null" )
78- : "null" ;
79- const about = contact ? await contact . getAbout ( ) . catch ( ( ) => null ) : null ;
98+ return true ;
99+ } ) ;
80100
81- await Promise . allSettled ( [
82- msg . react ( "✅" ) ,
83- prisma . user . create ( {
84- data : {
85- lid,
86- name,
87- number,
88- countryCode,
89- type : contact ?. isBusiness ? "business" : "private" ,
90- mode : msg . author ? "group" : "private" ,
91- about : about ? filterContent ( about ) : null ,
92- commandCount : 1 ,
93- points,
94- } ,
95- } ) ,
96- ] ) ;
97- return true ;
101+ return result ;
98102 } catch ( error ) {
99103 Sentry . captureException ( error ) ;
100104 log . error ( "Database" , `Failed to find or create user.` , error ) ;
105+ return false ;
101106 }
102- return false ;
103107}
104108
105109export async function getUserbyLid ( lid : string ) {
@@ -286,9 +290,11 @@ export async function deductUserPoints(
286290 points : number ,
287291) : Promise < void > {
288292 try {
289- prisma . user . update ( {
290- where : { lid } ,
291- data : { points : { decrement : points } } ,
293+ await prisma . $transaction ( async ( tx ) => {
294+ await tx . user . update ( {
295+ where : { lid } ,
296+ data : { points : { decrement : points } } ,
297+ } ) ;
292298 } ) ;
293299 } catch ( error ) {
294300 Sentry . captureException ( error ) ;
@@ -301,9 +307,11 @@ export async function addUserPoints(
301307 points : number ,
302308) : Promise < void > {
303309 try {
304- prisma . user . update ( {
305- where : { lid } ,
306- data : { points : { increment : points } } ,
310+ await prisma . $transaction ( async ( tx ) => {
311+ await tx . user . update ( {
312+ where : { lid } ,
313+ data : { points : { increment : points } } ,
314+ } ) ;
307315 } ) ;
308316 } catch ( error ) {
309317 Sentry . captureException ( error ) ;
0 commit comments