@@ -18,26 +18,22 @@ export const ecommerceRouter = router({
18
18
const { db, user } = ctx ;
19
19
try {
20
20
const existingEcommerceClient =
21
- await db . query . ecommerceClientTable . findMany ( {
21
+ await db . query . ecommerceClientTable . findFirst ( {
22
22
where : and (
23
23
eq ( ecommerceClientTable . userId , user . id ) ,
24
24
eq ( ecommerceClientTable . domain , input . domain ) ,
25
25
) ,
26
26
} ) ;
27
27
28
- if ( existingEcommerceClient . length > 0 ) {
28
+ if ( existingEcommerceClient ) {
29
29
throw new Error ( "Ecommerce client for this domain already exists." ) ;
30
30
}
31
31
32
32
const response = await apiClient . post ( "v2/client-ids" , {
33
33
label : input . label ,
34
34
allowedDomains : [ input . domain ] ,
35
- ...( input . feeAddress && input . feePercentage
36
- ? {
37
- feeAddress : input . feeAddress ,
38
- feePercentage : input . feePercentage ,
39
- }
40
- : { } ) ,
35
+ feePercentage : input . feePercentage ?? null ,
36
+ feeAddress : input . feeAddress ?? null ,
41
37
} ) ;
42
38
43
39
if ( ! response . data . clientId ) {
@@ -51,8 +47,8 @@ export const ecommerceRouter = router({
51
47
externalId : response . data . id ,
52
48
rnClientId : response . data . clientId ,
53
49
label : input . label ,
54
- feeAddress : input . feeAddress ,
55
- feePercentage : input . feePercentage ?. toString ( ) ?? undefined ,
50
+ feeAddress : input . feeAddress ?? null ,
51
+ feePercentage : input . feePercentage ?? null ,
56
52
} ) ;
57
53
} catch ( error ) {
58
54
throw toTRPCError ( error ) ;
@@ -73,20 +69,31 @@ export const ecommerceRouter = router({
73
69
} ) ;
74
70
75
71
if ( ! existingEcommerceClient ) {
76
- throw new Error ( "Client ID for this user doesn't exist." ) ;
72
+ throw new Error ( "Client not found or doesn't belong to this user." ) ;
73
+ }
74
+
75
+ if ( input . domain !== existingEcommerceClient . domain ) {
76
+ const conflictingClient =
77
+ await db . query . ecommerceClientTable . findFirst ( {
78
+ where : and (
79
+ eq ( ecommerceClientTable . userId , user . id ) ,
80
+ eq ( ecommerceClientTable . domain , input . domain ) ,
81
+ not ( eq ( ecommerceClientTable . id , input . id ) ) ,
82
+ ) ,
83
+ } ) ;
84
+
85
+ if ( conflictingClient ) {
86
+ throw new Error ( "Another client already exists for this domain." ) ;
87
+ }
77
88
}
78
89
79
90
await apiClient . put (
80
91
`v2/client-ids/${ existingEcommerceClient . externalId } ` ,
81
92
{
82
93
label : input . label ,
83
94
allowedDomains : [ input . domain ] ,
84
- ...( input . feeAddress && input . feePercentage
85
- ? {
86
- feeAddress : input . feeAddress ,
87
- feePercentage : input . feePercentage ,
88
- }
89
- : { } ) ,
95
+ feePercentage : input . feePercentage ?? null ,
96
+ feeAddress : input . feeAddress ?? null ,
90
97
} ,
91
98
) ;
92
99
@@ -95,8 +102,8 @@ export const ecommerceRouter = router({
95
102
. set ( {
96
103
label : input . label ,
97
104
domain : input . domain ,
98
- feeAddress : input . feeAddress ,
99
- feePercentage : input . feePercentage ?. toString ( ) ?? undefined ,
105
+ feeAddress : input . feeAddress ?? null ,
106
+ feePercentage : input . feePercentage ?? null ,
100
107
} )
101
108
. where ( eq ( ecommerceClientTable . id , input . id ) ) ;
102
109
} catch ( error ) {
@@ -131,13 +138,13 @@ export const ecommerceRouter = router({
131
138
throw new Error ( "Client ID not found." ) ;
132
139
}
133
140
134
- await apiClient . delete (
135
- `v2/client-ids/${ existingEcommerceClient . externalId } ` ,
136
- ) ;
137
-
138
141
await db
139
142
. delete ( ecommerceClientTable )
140
143
. where ( eq ( ecommerceClientTable . id , existingEcommerceClient . id ) ) ;
144
+
145
+ await apiClient . delete (
146
+ `v2/client-ids/${ existingEcommerceClient . externalId } ` ,
147
+ ) ;
141
148
} catch ( error ) {
142
149
throw toTRPCError ( error ) ;
143
150
}
0 commit comments