adaptic-backend 1.0.340 → 1.0.342

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. package/Account.cjs +2670 -2264
  2. package/Account.d.ts +19 -2
  3. package/Action.cjs +1195 -839
  4. package/Action.d.ts +19 -2
  5. package/Alert.cjs +2651 -2347
  6. package/Alert.d.ts +19 -2
  7. package/Allocation.cjs +2631 -2304
  8. package/Allocation.d.ts +19 -2
  9. package/AlpacaAccount.cjs +2654 -2104
  10. package/AlpacaAccount.d.ts +19 -2
  11. package/Asset.cjs +2611 -1544
  12. package/Asset.d.ts +19 -2
  13. package/Authenticator.cjs +2603 -2290
  14. package/Authenticator.d.ts +19 -2
  15. package/Customer.cjs +2506 -2220
  16. package/Customer.d.ts +19 -2
  17. package/EconomicEvent.cjs +708 -367
  18. package/EconomicEvent.d.ts +19 -2
  19. package/MarketSentiment.cjs +691 -340
  20. package/MarketSentiment.d.ts +19 -2
  21. package/NewsArticle.cjs +2114 -1655
  22. package/NewsArticle.d.ts +19 -2
  23. package/NewsArticleAssetSentiment.cjs +1873 -1564
  24. package/NewsArticleAssetSentiment.d.ts +19 -2
  25. package/ScheduledOptionOrder.cjs +607 -316
  26. package/ScheduledOptionOrder.d.ts +19 -2
  27. package/Session.cjs +2592 -2294
  28. package/Session.d.ts +19 -2
  29. package/Trade.cjs +1261 -795
  30. package/Trade.d.ts +19 -2
  31. package/User.cjs +2432 -2013
  32. package/User.d.ts +19 -2
  33. package/VerificationToken.cjs +637 -328
  34. package/VerificationToken.d.ts +19 -2
  35. package/esm/Account.d.ts +19 -2
  36. package/esm/Account.d.ts.map +1 -1
  37. package/esm/Account.js.map +1 -1
  38. package/esm/Account.mjs +2661 -2262
  39. package/esm/Action.d.ts +19 -2
  40. package/esm/Action.d.ts.map +1 -1
  41. package/esm/Action.js.map +1 -1
  42. package/esm/Action.mjs +1186 -837
  43. package/esm/Alert.d.ts +19 -2
  44. package/esm/Alert.d.ts.map +1 -1
  45. package/esm/Alert.js.map +1 -1
  46. package/esm/Alert.mjs +2642 -2345
  47. package/esm/Allocation.d.ts +19 -2
  48. package/esm/Allocation.d.ts.map +1 -1
  49. package/esm/Allocation.js.map +1 -1
  50. package/esm/Allocation.mjs +2622 -2302
  51. package/esm/AlpacaAccount.d.ts +19 -2
  52. package/esm/AlpacaAccount.d.ts.map +1 -1
  53. package/esm/AlpacaAccount.js.map +1 -1
  54. package/esm/AlpacaAccount.mjs +2645 -2102
  55. package/esm/Asset.d.ts +19 -2
  56. package/esm/Asset.d.ts.map +1 -1
  57. package/esm/Asset.js.map +1 -1
  58. package/esm/Asset.mjs +2602 -1542
  59. package/esm/Authenticator.d.ts +19 -2
  60. package/esm/Authenticator.d.ts.map +1 -1
  61. package/esm/Authenticator.js.map +1 -1
  62. package/esm/Authenticator.mjs +2594 -2288
  63. package/esm/Customer.d.ts +19 -2
  64. package/esm/Customer.d.ts.map +1 -1
  65. package/esm/Customer.js.map +1 -1
  66. package/esm/Customer.mjs +2497 -2218
  67. package/esm/EconomicEvent.d.ts +19 -2
  68. package/esm/EconomicEvent.d.ts.map +1 -1
  69. package/esm/EconomicEvent.js.map +1 -1
  70. package/esm/EconomicEvent.mjs +699 -365
  71. package/esm/MarketSentiment.d.ts +19 -2
  72. package/esm/MarketSentiment.d.ts.map +1 -1
  73. package/esm/MarketSentiment.js.map +1 -1
  74. package/esm/MarketSentiment.mjs +682 -338
  75. package/esm/NewsArticle.d.ts +19 -2
  76. package/esm/NewsArticle.d.ts.map +1 -1
  77. package/esm/NewsArticle.js.map +1 -1
  78. package/esm/NewsArticle.mjs +2105 -1653
  79. package/esm/NewsArticleAssetSentiment.d.ts +19 -2
  80. package/esm/NewsArticleAssetSentiment.d.ts.map +1 -1
  81. package/esm/NewsArticleAssetSentiment.js.map +1 -1
  82. package/esm/NewsArticleAssetSentiment.mjs +1864 -1562
  83. package/esm/ScheduledOptionOrder.d.ts +19 -2
  84. package/esm/ScheduledOptionOrder.d.ts.map +1 -1
  85. package/esm/ScheduledOptionOrder.js.map +1 -1
  86. package/esm/ScheduledOptionOrder.mjs +598 -314
  87. package/esm/Session.d.ts +19 -2
  88. package/esm/Session.d.ts.map +1 -1
  89. package/esm/Session.js.map +1 -1
  90. package/esm/Session.mjs +2583 -2292
  91. package/esm/Trade.d.ts +19 -2
  92. package/esm/Trade.d.ts.map +1 -1
  93. package/esm/Trade.js.map +1 -1
  94. package/esm/Trade.mjs +1252 -793
  95. package/esm/User.d.ts +19 -2
  96. package/esm/User.d.ts.map +1 -1
  97. package/esm/User.js.map +1 -1
  98. package/esm/User.mjs +2423 -2011
  99. package/esm/VerificationToken.d.ts +19 -2
  100. package/esm/VerificationToken.d.ts.map +1 -1
  101. package/esm/VerificationToken.js.map +1 -1
  102. package/esm/VerificationToken.mjs +628 -326
  103. package/esm/generated/typegraphql-prisma/enhance.js.map +1 -1
  104. package/esm/generated/typegraphql-prisma/enhance.mjs +1 -1
  105. package/esm/generated/typegraphql-prisma/resolvers/inputs/AccountWhereUniqueInput.d.ts +1 -1
  106. package/esm/generated/typegraphql-prisma/resolvers/inputs/AccountWhereUniqueInput.d.ts.map +1 -1
  107. package/esm/generated/typegraphql-prisma/resolvers/inputs/AccountWhereUniqueInput.js.map +1 -1
  108. package/esm/generated/typegraphql-prisma/resolvers/inputs/AccountWhereUniqueInput.mjs +7 -7
  109. package/esm/prismaClient.d.ts +4 -5
  110. package/esm/prismaClient.d.ts.map +1 -1
  111. package/esm/prismaClient.js.map +1 -1
  112. package/esm/prismaClient.mjs +13 -11
  113. package/generated/typegraphql-prisma/enhance.cjs +1 -1
  114. package/generated/typegraphql-prisma/enhance.js.map +1 -1
  115. package/generated/typegraphql-prisma/resolvers/inputs/AccountWhereUniqueInput.cjs +6 -6
  116. package/generated/typegraphql-prisma/resolvers/inputs/AccountWhereUniqueInput.d.ts +1 -1
  117. package/generated/typegraphql-prisma/resolvers/inputs/AccountWhereUniqueInput.d.ts.map +1 -1
  118. package/generated/typegraphql-prisma/resolvers/inputs/AccountWhereUniqueInput.js.map +1 -1
  119. package/package.json +1 -1
  120. package/prismaClient.cjs +13 -11
  121. package/prismaClient.d.ts +4 -5
  122. package/server.cjs +37 -5
package/esm/User.mjs CHANGED
@@ -123,183 +123,755 @@ export const User = {
123
123
  * @param client - Apollo Client instance.
124
124
  * @returns The created User or null.
125
125
  */
126
+ /**
127
+ * Create a new User record.
128
+ * Enhanced with connection resilience against Prisma connection errors.
129
+ * @param props - Properties for the new record.
130
+ * @param globalClient - Apollo Client instance.
131
+ * @returns The created User or null.
132
+ */
126
133
  async create(props, globalClient) {
127
- const [modules, client] = await Promise.all([
128
- getApolloModules(),
129
- globalClient
130
- ? Promise.resolve(globalClient)
131
- : importedClient
132
- ]);
133
- const { gql, ApolloError } = modules;
134
- const CREATE_ONE_USER = gql `
135
- mutation createOneUser($data: UserCreateInput!) {
136
- createOneUser(data: $data) {
137
- ${selectionSet}
138
- }
139
- }
140
- `;
141
- const variables = {
142
- data: {
143
- name: props.name !== undefined ? props.name : undefined,
144
- email: props.email !== undefined ? props.email : undefined,
145
- emailVerified: props.emailVerified !== undefined ? props.emailVerified : undefined,
146
- image: props.image !== undefined ? props.image : undefined,
147
- role: props.role !== undefined ? props.role : undefined,
148
- bio: props.bio !== undefined ? props.bio : undefined,
149
- jobTitle: props.jobTitle !== undefined ? props.jobTitle : undefined,
150
- currentAccount: props.currentAccount !== undefined ? props.currentAccount : undefined,
151
- plan: props.plan !== undefined ? props.plan : undefined,
152
- openaiAPIKey: props.openaiAPIKey !== undefined ? props.openaiAPIKey : undefined,
153
- openaiModel: props.openaiModel !== undefined ? props.openaiModel : undefined,
154
- customer: props.customer ?
155
- typeof props.customer === 'object' && Object.keys(props.customer).length === 1 && Object.keys(props.customer)[0] === 'id'
156
- ? { connect: {
157
- id: props.customer.id
134
+ // Maximum number of retries for database connection issues
135
+ const MAX_RETRIES = 3;
136
+ let retryCount = 0;
137
+ let lastError = null;
138
+ // Retry loop to handle potential database connection issues
139
+ while (retryCount < MAX_RETRIES) {
140
+ try {
141
+ const [modules, client] = await Promise.all([
142
+ getApolloModules(),
143
+ globalClient
144
+ ? Promise.resolve(globalClient)
145
+ : importedClient
146
+ ]);
147
+ const { gql, ApolloError } = modules;
148
+ const CREATE_ONE_USER = gql `
149
+ mutation createOneUser($data: UserCreateInput!) {
150
+ createOneUser(data: $data) {
151
+ ${selectionSet}
152
+ }
153
+ }
154
+ `;
155
+ const variables = {
156
+ data: {
157
+ name: props.name !== undefined ? props.name : undefined,
158
+ email: props.email !== undefined ? props.email : undefined,
159
+ emailVerified: props.emailVerified !== undefined ? props.emailVerified : undefined,
160
+ image: props.image !== undefined ? props.image : undefined,
161
+ role: props.role !== undefined ? props.role : undefined,
162
+ bio: props.bio !== undefined ? props.bio : undefined,
163
+ jobTitle: props.jobTitle !== undefined ? props.jobTitle : undefined,
164
+ currentAccount: props.currentAccount !== undefined ? props.currentAccount : undefined,
165
+ plan: props.plan !== undefined ? props.plan : undefined,
166
+ openaiAPIKey: props.openaiAPIKey !== undefined ? props.openaiAPIKey : undefined,
167
+ openaiModel: props.openaiModel !== undefined ? props.openaiModel : undefined,
168
+ customer: props.customer ?
169
+ typeof props.customer === 'object' && Object.keys(props.customer).length === 1 && Object.keys(props.customer)[0] === 'id'
170
+ ? { connect: {
171
+ id: props.customer.id
172
+ }
173
+ }
174
+ : { connectOrCreate: {
175
+ where: {
176
+ id: props.customer.id !== undefined ? props.customer.id : undefined,
177
+ stripeCustomerId: props.customer.stripeCustomerId !== undefined ? props.customer.stripeCustomerId : undefined,
178
+ stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? props.customer.stripeSubscriptionId : undefined,
179
+ authUserId: props.customer.authUserId !== undefined ? {
180
+ equals: props.customer.authUserId
181
+ } : undefined,
182
+ name: props.customer.name !== undefined ? {
183
+ equals: props.customer.name
184
+ } : undefined,
185
+ stripePriceId: props.customer.stripePriceId !== undefined ? {
186
+ equals: props.customer.stripePriceId
187
+ } : undefined,
188
+ },
189
+ create: {
190
+ authUserId: props.customer.authUserId !== undefined ? props.customer.authUserId : undefined,
191
+ name: props.customer.name !== undefined ? props.customer.name : undefined,
192
+ plan: props.customer.plan !== undefined ? props.customer.plan : undefined,
193
+ stripeCustomerId: props.customer.stripeCustomerId !== undefined ? props.customer.stripeCustomerId : undefined,
194
+ stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? props.customer.stripeSubscriptionId : undefined,
195
+ stripePriceId: props.customer.stripePriceId !== undefined ? props.customer.stripePriceId : undefined,
196
+ stripeCurrentPeriodEnd: props.customer.stripeCurrentPeriodEnd !== undefined ? props.customer.stripeCurrentPeriodEnd : undefined,
197
+ },
198
+ }
199
+ } : undefined,
200
+ accounts: props.accounts ?
201
+ Array.isArray(props.accounts) && props.accounts.length > 0 && props.accounts.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
202
+ connect: props.accounts.map((item) => ({
203
+ id: item.id
204
+ }))
158
205
  }
159
- }
160
- : { connectOrCreate: {
161
- where: {
162
- id: props.customer.id !== undefined ? props.customer.id : undefined,
163
- stripeCustomerId: props.customer.stripeCustomerId !== undefined ? props.customer.stripeCustomerId : undefined,
164
- stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? props.customer.stripeSubscriptionId : undefined,
165
- authUserId: props.customer.authUserId !== undefined ? {
166
- equals: props.customer.authUserId
167
- } : undefined,
168
- name: props.customer.name !== undefined ? {
169
- equals: props.customer.name
170
- } : undefined,
171
- stripePriceId: props.customer.stripePriceId !== undefined ? {
172
- equals: props.customer.stripePriceId
173
- } : undefined,
174
- },
175
- create: {
176
- authUserId: props.customer.authUserId !== undefined ? props.customer.authUserId : undefined,
177
- name: props.customer.name !== undefined ? props.customer.name : undefined,
178
- plan: props.customer.plan !== undefined ? props.customer.plan : undefined,
179
- stripeCustomerId: props.customer.stripeCustomerId !== undefined ? props.customer.stripeCustomerId : undefined,
180
- stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? props.customer.stripeSubscriptionId : undefined,
181
- stripePriceId: props.customer.stripePriceId !== undefined ? props.customer.stripePriceId : undefined,
182
- stripeCurrentPeriodEnd: props.customer.stripeCurrentPeriodEnd !== undefined ? props.customer.stripeCurrentPeriodEnd : undefined,
183
- },
206
+ : { connectOrCreate: props.accounts.map((item) => ({
207
+ where: {
208
+ id: item.id !== undefined ? item.id : undefined,
209
+ providerAccountId: item.providerAccountId !== undefined ? item.providerAccountId : undefined,
210
+ userId: item.userId !== undefined ? {
211
+ equals: item.userId
212
+ } : undefined,
213
+ },
214
+ create: {
215
+ type: item.type !== undefined ? item.type : undefined,
216
+ provider: item.provider !== undefined ? item.provider : undefined,
217
+ providerAccountId: item.providerAccountId !== undefined ? item.providerAccountId : undefined,
218
+ refresh_token: item.refresh_token !== undefined ? item.refresh_token : undefined,
219
+ access_token: item.access_token !== undefined ? item.access_token : undefined,
220
+ expires_at: item.expires_at !== undefined ? item.expires_at : undefined,
221
+ token_type: item.token_type !== undefined ? item.token_type : undefined,
222
+ scope: item.scope !== undefined ? item.scope : undefined,
223
+ id_token: item.id_token !== undefined ? item.id_token : undefined,
224
+ session_state: item.session_state !== undefined ? item.session_state : undefined,
225
+ },
226
+ }))
227
+ } : undefined,
228
+ sessions: props.sessions ?
229
+ Array.isArray(props.sessions) && props.sessions.length > 0 && props.sessions.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
230
+ connect: props.sessions.map((item) => ({
231
+ id: item.id
232
+ }))
184
233
  }
185
- } : undefined,
186
- accounts: props.accounts ?
187
- Array.isArray(props.accounts) && props.accounts.length > 0 && props.accounts.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
188
- connect: props.accounts.map((item) => ({
189
- id: item.id
190
- }))
191
- }
192
- : { connectOrCreate: props.accounts.map((item) => ({
193
- where: {
194
- id: item.id !== undefined ? item.id : undefined,
195
- userId: item.userId !== undefined ? {
196
- equals: item.userId
197
- } : undefined,
198
- providerAccountId: item.providerAccountId !== undefined ? {
199
- equals: item.providerAccountId
200
- } : undefined,
201
- },
202
- create: {
203
- type: item.type !== undefined ? item.type : undefined,
204
- provider: item.provider !== undefined ? item.provider : undefined,
205
- providerAccountId: item.providerAccountId !== undefined ? item.providerAccountId : undefined,
206
- refresh_token: item.refresh_token !== undefined ? item.refresh_token : undefined,
207
- access_token: item.access_token !== undefined ? item.access_token : undefined,
208
- expires_at: item.expires_at !== undefined ? item.expires_at : undefined,
209
- token_type: item.token_type !== undefined ? item.token_type : undefined,
210
- scope: item.scope !== undefined ? item.scope : undefined,
211
- id_token: item.id_token !== undefined ? item.id_token : undefined,
212
- session_state: item.session_state !== undefined ? item.session_state : undefined,
213
- },
214
- }))
215
- } : undefined,
216
- sessions: props.sessions ?
217
- Array.isArray(props.sessions) && props.sessions.length > 0 && props.sessions.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
218
- connect: props.sessions.map((item) => ({
219
- id: item.id
220
- }))
221
- }
222
- : { connectOrCreate: props.sessions.map((item) => ({
223
- where: {
224
- id: item.id !== undefined ? item.id : undefined,
225
- userId: item.userId !== undefined ? {
226
- equals: item.userId
227
- } : undefined,
228
- },
229
- create: {
230
- sessionToken: item.sessionToken !== undefined ? item.sessionToken : undefined,
231
- expires: item.expires !== undefined ? item.expires : undefined,
232
- },
233
- }))
234
- } : undefined,
235
- authenticators: props.authenticators ?
236
- Array.isArray(props.authenticators) && props.authenticators.length > 0 && props.authenticators.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
237
- connect: props.authenticators.map((item) => ({
238
- id: item.id
239
- }))
240
- }
241
- : { connectOrCreate: props.authenticators.map((item) => ({
242
- where: {
243
- id: item.id !== undefined ? item.id : undefined,
244
- userId: item.userId !== undefined ? {
245
- equals: item.userId
246
- } : undefined,
247
- },
248
- create: {
249
- credentialID: item.credentialID !== undefined ? item.credentialID : undefined,
250
- publicKey: item.publicKey !== undefined ? item.publicKey : undefined,
251
- counter: item.counter !== undefined ? item.counter : undefined,
252
- },
253
- }))
254
- } : undefined,
255
- alpacaAccounts: props.alpacaAccounts ?
256
- Array.isArray(props.alpacaAccounts) && props.alpacaAccounts.length > 0 && props.alpacaAccounts.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
257
- connect: props.alpacaAccounts.map((item) => ({
258
- id: item.id
259
- }))
260
- }
261
- : { connectOrCreate: props.alpacaAccounts.map((item) => ({
262
- where: {
263
- id: item.id !== undefined ? item.id : undefined,
264
- userId: item.userId !== undefined ? {
265
- equals: item.userId
266
- } : undefined,
267
- },
268
- create: {
269
- type: item.type !== undefined ? item.type : undefined,
270
- APIKey: item.APIKey !== undefined ? item.APIKey : undefined,
271
- APISecret: item.APISecret !== undefined ? item.APISecret : undefined,
272
- configuration: item.configuration !== undefined ? item.configuration : undefined,
273
- marketOpen: item.marketOpen !== undefined ? item.marketOpen : undefined,
274
- realTime: item.realTime !== undefined ? item.realTime : undefined,
275
- cryptoTradingEnabled: item.cryptoTradingEnabled !== undefined ? item.cryptoTradingEnabled : undefined,
276
- cryptoTradingPairs: item.cryptoTradingPairs !== undefined ? {
277
- set: item.cryptoTradingPairs
278
- } : undefined,
279
- cryptoTradeAllocationPct: item.cryptoTradeAllocationPct !== undefined ? item.cryptoTradeAllocationPct : undefined,
280
- tradeAllocationPct: item.tradeAllocationPct !== undefined ? item.tradeAllocationPct : undefined,
281
- minPercentageChange: item.minPercentageChange !== undefined ? item.minPercentageChange : undefined,
282
- volumeThreshold: item.volumeThreshold !== undefined ? item.volumeThreshold : undefined,
283
- enablePortfolioTrailingStop: item.enablePortfolioTrailingStop !== undefined ? item.enablePortfolioTrailingStop : undefined,
284
- portfolioTrailPercent: item.portfolioTrailPercent !== undefined ? item.portfolioTrailPercent : undefined,
285
- portfolioProfitThresholdPercent: item.portfolioProfitThresholdPercent !== undefined ? item.portfolioProfitThresholdPercent : undefined,
286
- reducedPortfolioTrailPercent: item.reducedPortfolioTrailPercent !== undefined ? item.reducedPortfolioTrailPercent : undefined,
287
- defaultTrailingStopPercentage100: item.defaultTrailingStopPercentage100 !== undefined ? item.defaultTrailingStopPercentage100 : undefined,
288
- firstTrailReductionThreshold100: item.firstTrailReductionThreshold100 !== undefined ? item.firstTrailReductionThreshold100 : undefined,
289
- secondTrailReductionThreshold100: item.secondTrailReductionThreshold100 !== undefined ? item.secondTrailReductionThreshold100 : undefined,
290
- firstReducedTrailPercentage100: item.firstReducedTrailPercentage100 !== undefined ? item.firstReducedTrailPercentage100 : undefined,
291
- secondReducedTrailPercentage100: item.secondReducedTrailPercentage100 !== undefined ? item.secondReducedTrailPercentage100 : undefined,
292
- minimumPriceChangePercent100: item.minimumPriceChangePercent100 !== undefined ? item.minimumPriceChangePercent100 : undefined,
293
- allocation: item.allocation ?
294
- typeof item.allocation === 'object' && Object.keys(item.allocation).length === 1 && Object.keys(item.allocation)[0] === 'id'
295
- ? { connect: {
296
- id: item.allocation.id
234
+ : { connectOrCreate: props.sessions.map((item) => ({
235
+ where: {
236
+ id: item.id !== undefined ? item.id : undefined,
237
+ userId: item.userId !== undefined ? {
238
+ equals: item.userId
239
+ } : undefined,
240
+ },
241
+ create: {
242
+ sessionToken: item.sessionToken !== undefined ? item.sessionToken : undefined,
243
+ expires: item.expires !== undefined ? item.expires : undefined,
244
+ },
245
+ }))
246
+ } : undefined,
247
+ authenticators: props.authenticators ?
248
+ Array.isArray(props.authenticators) && props.authenticators.length > 0 && props.authenticators.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
249
+ connect: props.authenticators.map((item) => ({
250
+ id: item.id
251
+ }))
252
+ }
253
+ : { connectOrCreate: props.authenticators.map((item) => ({
254
+ where: {
255
+ id: item.id !== undefined ? item.id : undefined,
256
+ userId: item.userId !== undefined ? {
257
+ equals: item.userId
258
+ } : undefined,
259
+ },
260
+ create: {
261
+ credentialID: item.credentialID !== undefined ? item.credentialID : undefined,
262
+ publicKey: item.publicKey !== undefined ? item.publicKey : undefined,
263
+ counter: item.counter !== undefined ? item.counter : undefined,
264
+ },
265
+ }))
266
+ } : undefined,
267
+ alpacaAccounts: props.alpacaAccounts ?
268
+ Array.isArray(props.alpacaAccounts) && props.alpacaAccounts.length > 0 && props.alpacaAccounts.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
269
+ connect: props.alpacaAccounts.map((item) => ({
270
+ id: item.id
271
+ }))
272
+ }
273
+ : { connectOrCreate: props.alpacaAccounts.map((item) => ({
274
+ where: {
275
+ id: item.id !== undefined ? item.id : undefined,
276
+ userId: item.userId !== undefined ? {
277
+ equals: item.userId
278
+ } : undefined,
279
+ },
280
+ create: {
281
+ type: item.type !== undefined ? item.type : undefined,
282
+ APIKey: item.APIKey !== undefined ? item.APIKey : undefined,
283
+ APISecret: item.APISecret !== undefined ? item.APISecret : undefined,
284
+ configuration: item.configuration !== undefined ? item.configuration : undefined,
285
+ marketOpen: item.marketOpen !== undefined ? item.marketOpen : undefined,
286
+ realTime: item.realTime !== undefined ? item.realTime : undefined,
287
+ cryptoTradingEnabled: item.cryptoTradingEnabled !== undefined ? item.cryptoTradingEnabled : undefined,
288
+ cryptoTradingPairs: item.cryptoTradingPairs !== undefined ? {
289
+ set: item.cryptoTradingPairs
290
+ } : undefined,
291
+ cryptoTradeAllocationPct: item.cryptoTradeAllocationPct !== undefined ? item.cryptoTradeAllocationPct : undefined,
292
+ tradeAllocationPct: item.tradeAllocationPct !== undefined ? item.tradeAllocationPct : undefined,
293
+ minPercentageChange: item.minPercentageChange !== undefined ? item.minPercentageChange : undefined,
294
+ volumeThreshold: item.volumeThreshold !== undefined ? item.volumeThreshold : undefined,
295
+ enablePortfolioTrailingStop: item.enablePortfolioTrailingStop !== undefined ? item.enablePortfolioTrailingStop : undefined,
296
+ portfolioTrailPercent: item.portfolioTrailPercent !== undefined ? item.portfolioTrailPercent : undefined,
297
+ portfolioProfitThresholdPercent: item.portfolioProfitThresholdPercent !== undefined ? item.portfolioProfitThresholdPercent : undefined,
298
+ reducedPortfolioTrailPercent: item.reducedPortfolioTrailPercent !== undefined ? item.reducedPortfolioTrailPercent : undefined,
299
+ defaultTrailingStopPercentage100: item.defaultTrailingStopPercentage100 !== undefined ? item.defaultTrailingStopPercentage100 : undefined,
300
+ firstTrailReductionThreshold100: item.firstTrailReductionThreshold100 !== undefined ? item.firstTrailReductionThreshold100 : undefined,
301
+ secondTrailReductionThreshold100: item.secondTrailReductionThreshold100 !== undefined ? item.secondTrailReductionThreshold100 : undefined,
302
+ firstReducedTrailPercentage100: item.firstReducedTrailPercentage100 !== undefined ? item.firstReducedTrailPercentage100 : undefined,
303
+ secondReducedTrailPercentage100: item.secondReducedTrailPercentage100 !== undefined ? item.secondReducedTrailPercentage100 : undefined,
304
+ minimumPriceChangePercent100: item.minimumPriceChangePercent100 !== undefined ? item.minimumPriceChangePercent100 : undefined,
305
+ allocation: item.allocation ?
306
+ typeof item.allocation === 'object' && Object.keys(item.allocation).length === 1 && Object.keys(item.allocation)[0] === 'id'
307
+ ? { connect: {
308
+ id: item.allocation.id
309
+ }
310
+ }
311
+ : { connectOrCreate: {
312
+ where: {
313
+ id: item.allocation.id !== undefined ? item.allocation.id : undefined,
314
+ alpacaAccountId: item.allocation.alpacaAccountId !== undefined ? item.allocation.alpacaAccountId : undefined,
315
+ },
316
+ create: {
317
+ stocks: item.allocation.stocks !== undefined ? item.allocation.stocks : undefined,
318
+ crypto: item.allocation.crypto !== undefined ? item.allocation.crypto : undefined,
319
+ etfs: item.allocation.etfs !== undefined ? item.allocation.etfs : undefined,
320
+ },
321
+ }
322
+ } : undefined,
323
+ alerts: item.alerts ?
324
+ Array.isArray(item.alerts) && item.alerts.length > 0 && item.alerts.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
325
+ connect: item.alerts.map((item) => ({
326
+ id: item.id
327
+ }))
297
328
  }
298
- }
299
- : { connectOrCreate: {
329
+ : { connectOrCreate: item.alerts.map((item) => ({
330
+ where: {
331
+ id: item.id !== undefined ? item.id : undefined,
332
+ alpacaAccountId: item.alpacaAccountId !== undefined ? {
333
+ equals: item.alpacaAccountId
334
+ } : undefined,
335
+ },
336
+ create: {
337
+ message: item.message !== undefined ? item.message : undefined,
338
+ type: item.type !== undefined ? item.type : undefined,
339
+ isRead: item.isRead !== undefined ? item.isRead : undefined,
340
+ },
341
+ }))
342
+ } : undefined,
343
+ },
344
+ }))
345
+ } : undefined,
346
+ },
347
+ };
348
+ const filteredVariables = removeUndefinedProps(variables);
349
+ const response = await client.mutate({
350
+ mutation: CREATE_ONE_USER,
351
+ variables: filteredVariables,
352
+ // Don't cache mutations, but ensure we're using the freshest context
353
+ fetchPolicy: 'no-cache'
354
+ });
355
+ if (response.errors && response.errors.length > 0)
356
+ throw new Error(response.errors[0].message);
357
+ if (response && response.data && response.data.createOneUser) {
358
+ return response.data.createOneUser;
359
+ }
360
+ else {
361
+ return null;
362
+ }
363
+ }
364
+ catch (error) {
365
+ lastError = error;
366
+ // Check if this is a database connection error that we should retry
367
+ const isConnectionError = error.message?.includes('Server has closed the connection') ||
368
+ error.message?.includes('Cannot reach database server') ||
369
+ error.message?.includes('Connection timed out') ||
370
+ error.message?.includes('Accelerate') || // Prisma Accelerate proxy errors
371
+ (error.networkError && error.networkError.message?.includes('Failed to fetch'));
372
+ if (isConnectionError && retryCount < MAX_RETRIES - 1) {
373
+ retryCount++;
374
+ const delay = Math.pow(2, retryCount) * 100; // Exponential backoff: 200ms, 400ms, 800ms
375
+ console.warn("Database connection error, retrying...");
376
+ await new Promise(resolve => setTimeout(resolve, delay));
377
+ continue;
378
+ }
379
+ // Log the error and rethrow
380
+ console.error("Database error occurred:", error);
381
+ throw error;
382
+ }
383
+ }
384
+ // If we exhausted retries, throw the last error
385
+ throw lastError;
386
+ },
387
+ /**
388
+ * Create multiple User records.
389
+ * Enhanced with connection resilience against Prisma connection errors.
390
+ * @param props - Array of User objects for the new records.
391
+ * @param globalClient - Apollo Client instance.
392
+ * @returns The count of created records or null.
393
+ */
394
+ async createMany(props, globalClient) {
395
+ // Maximum number of retries for database connection issues
396
+ const MAX_RETRIES = 3;
397
+ let retryCount = 0;
398
+ let lastError = null;
399
+ // Retry loop to handle potential database connection issues
400
+ while (retryCount < MAX_RETRIES) {
401
+ try {
402
+ const [modules, client] = await Promise.all([
403
+ getApolloModules(),
404
+ globalClient
405
+ ? Promise.resolve(globalClient)
406
+ : importedClient
407
+ ]);
408
+ const { gql, ApolloError } = modules;
409
+ const CREATE_MANY_USER = gql `
410
+ mutation createManyUser($data: [UserCreateManyInput!]!) {
411
+ createManyUser(data: $data) {
412
+ count
413
+ }
414
+ }`;
415
+ const variables = {
416
+ data: props.map(prop => ({
417
+ name: prop.name !== undefined ? prop.name : undefined,
418
+ email: prop.email !== undefined ? prop.email : undefined,
419
+ emailVerified: prop.emailVerified !== undefined ? prop.emailVerified : undefined,
420
+ image: prop.image !== undefined ? prop.image : undefined,
421
+ role: prop.role !== undefined ? prop.role : undefined,
422
+ bio: prop.bio !== undefined ? prop.bio : undefined,
423
+ jobTitle: prop.jobTitle !== undefined ? prop.jobTitle : undefined,
424
+ currentAccount: prop.currentAccount !== undefined ? prop.currentAccount : undefined,
425
+ customerId: prop.customerId !== undefined ? prop.customerId : undefined,
426
+ plan: prop.plan !== undefined ? prop.plan : undefined,
427
+ openaiAPIKey: prop.openaiAPIKey !== undefined ? prop.openaiAPIKey : undefined,
428
+ openaiModel: prop.openaiModel !== undefined ? prop.openaiModel : undefined,
429
+ })),
430
+ };
431
+ const filteredVariables = removeUndefinedProps(variables);
432
+ const response = await client.mutate({
433
+ mutation: CREATE_MANY_USER,
434
+ variables: filteredVariables,
435
+ // Don't cache mutations, but ensure we're using the freshest context
436
+ fetchPolicy: 'no-cache'
437
+ });
438
+ if (response.errors && response.errors.length > 0)
439
+ throw new Error(response.errors[0].message);
440
+ if (response && response.data && response.data.createManyUser) {
441
+ return response.data.createManyUser;
442
+ }
443
+ else {
444
+ return null;
445
+ }
446
+ }
447
+ catch (error) {
448
+ lastError = error;
449
+ // Check if this is a database connection error that we should retry
450
+ const isConnectionError = error.message?.includes('Server has closed the connection') ||
451
+ error.message?.includes('Cannot reach database server') ||
452
+ error.message?.includes('Connection timed out') ||
453
+ error.message?.includes('Accelerate') || // Prisma Accelerate proxy errors
454
+ (error.networkError && error.networkError.message?.includes('Failed to fetch'));
455
+ if (isConnectionError && retryCount < MAX_RETRIES - 1) {
456
+ retryCount++;
457
+ const delay = Math.pow(2, retryCount) * 100; // Exponential backoff: 200ms, 400ms, 800ms
458
+ console.warn("Database connection error, retrying...");
459
+ await new Promise(resolve => setTimeout(resolve, delay));
460
+ continue;
461
+ }
462
+ // Log the error and rethrow
463
+ console.error("Database error occurred:", error);
464
+ throw error;
465
+ }
466
+ }
467
+ // If we exhausted retries, throw the last error
468
+ throw lastError;
469
+ },
470
+ /**
471
+ * Update a single User record.
472
+ * Enhanced with connection resilience against Prisma connection errors.
473
+ * @param props - Properties to update.
474
+ * @param globalClient - Apollo Client instance.
475
+ * @returns The updated User or null.
476
+ */
477
+ async update(props, globalClient) {
478
+ // Maximum number of retries for database connection issues
479
+ const MAX_RETRIES = 3;
480
+ let retryCount = 0;
481
+ let lastError = null;
482
+ // Retry loop to handle potential database connection issues
483
+ while (retryCount < MAX_RETRIES) {
484
+ try {
485
+ const [modules, client] = await Promise.all([
486
+ getApolloModules(),
487
+ globalClient
488
+ ? Promise.resolve(globalClient)
489
+ : importedClient
490
+ ]);
491
+ const { gql, ApolloError } = modules;
492
+ const UPDATE_ONE_USER = gql `
493
+ mutation updateOneUser($data: UserUpdateInput!, $where: UserWhereUniqueInput!) {
494
+ updateOneUser(data: $data, where: $where) {
495
+ ${selectionSet}
496
+ }
497
+ }`;
498
+ const variables = {
499
+ where: {
500
+ id: props.id !== undefined ? props.id : undefined,
501
+ email: props.email !== undefined ? props.email : undefined,
502
+ name: props.name !== undefined ? props.name : undefined,
503
+ emailVerified: props.emailVerified !== undefined ? {
504
+ equals: props.emailVerified
505
+ } : undefined,
506
+ image: props.image !== undefined ? {
507
+ equals: props.image
508
+ } : undefined,
509
+ createdAt: props.createdAt !== undefined ? {
510
+ equals: props.createdAt
511
+ } : undefined,
512
+ updatedAt: props.updatedAt !== undefined ? {
513
+ equals: props.updatedAt
514
+ } : undefined,
515
+ role: props.role !== undefined ? {
516
+ equals: props.role
517
+ } : undefined,
518
+ bio: props.bio !== undefined ? {
519
+ equals: props.bio
520
+ } : undefined,
521
+ jobTitle: props.jobTitle !== undefined ? {
522
+ equals: props.jobTitle
523
+ } : undefined,
524
+ currentAccount: props.currentAccount !== undefined ? {
525
+ equals: props.currentAccount
526
+ } : undefined,
527
+ plan: props.plan !== undefined ? {
528
+ equals: props.plan
529
+ } : undefined,
530
+ openaiAPIKey: props.openaiAPIKey !== undefined ? {
531
+ equals: props.openaiAPIKey
532
+ } : undefined,
533
+ openaiModel: props.openaiModel !== undefined ? {
534
+ equals: props.openaiModel
535
+ } : undefined,
536
+ },
537
+ data: {
538
+ id: props.id !== undefined ? {
539
+ set: props.id
540
+ } : undefined,
541
+ name: props.name !== undefined ? {
542
+ set: props.name
543
+ } : undefined,
544
+ email: props.email !== undefined ? {
545
+ set: props.email
546
+ } : undefined,
547
+ emailVerified: props.emailVerified !== undefined ? {
548
+ set: props.emailVerified
549
+ } : undefined,
550
+ image: props.image !== undefined ? {
551
+ set: props.image
552
+ } : undefined,
553
+ createdAt: props.createdAt !== undefined ? {
554
+ set: props.createdAt
555
+ } : undefined,
556
+ updatedAt: props.updatedAt !== undefined ? {
557
+ set: props.updatedAt
558
+ } : undefined,
559
+ role: props.role !== undefined ? {
560
+ set: props.role
561
+ } : undefined,
562
+ bio: props.bio !== undefined ? {
563
+ set: props.bio
564
+ } : undefined,
565
+ jobTitle: props.jobTitle !== undefined ? {
566
+ set: props.jobTitle
567
+ } : undefined,
568
+ currentAccount: props.currentAccount !== undefined ? {
569
+ set: props.currentAccount
570
+ } : undefined,
571
+ plan: props.plan !== undefined ? {
572
+ set: props.plan
573
+ } : undefined,
574
+ openaiAPIKey: props.openaiAPIKey !== undefined ? {
575
+ set: props.openaiAPIKey
576
+ } : undefined,
577
+ openaiModel: props.openaiModel !== undefined ? {
578
+ set: props.openaiModel
579
+ } : undefined,
580
+ customer: props.customer ?
581
+ typeof props.customer === 'object' && Object.keys(props.customer).length === 1 && (Object.keys(props.customer)[0] === 'id' || Object.keys(props.customer)[0] === 'symbol')
582
+ ? {
583
+ connect: {
584
+ id: props.customer.id
585
+ }
586
+ } : { upsert: {
587
+ where: {
588
+ id: props.customer.id !== undefined ? {
589
+ equals: props.customer.id
590
+ } : undefined,
591
+ authUserId: props.customer.authUserId !== undefined ? {
592
+ equals: props.customer.authUserId
593
+ } : undefined,
594
+ name: props.customer.name !== undefined ? {
595
+ equals: props.customer.name
596
+ } : undefined,
597
+ stripeCustomerId: props.customer.stripeCustomerId !== undefined ? {
598
+ equals: props.customer.stripeCustomerId
599
+ } : undefined,
600
+ stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? {
601
+ equals: props.customer.stripeSubscriptionId
602
+ } : undefined,
603
+ stripePriceId: props.customer.stripePriceId !== undefined ? {
604
+ equals: props.customer.stripePriceId
605
+ } : undefined,
606
+ },
607
+ update: {
608
+ authUserId: props.customer.authUserId !== undefined ? {
609
+ set: props.customer.authUserId
610
+ } : undefined,
611
+ name: props.customer.name !== undefined ? {
612
+ set: props.customer.name
613
+ } : undefined,
614
+ plan: props.customer.plan !== undefined ? {
615
+ set: props.customer.plan
616
+ } : undefined,
617
+ stripeCustomerId: props.customer.stripeCustomerId !== undefined ? {
618
+ set: props.customer.stripeCustomerId
619
+ } : undefined,
620
+ stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? {
621
+ set: props.customer.stripeSubscriptionId
622
+ } : undefined,
623
+ stripePriceId: props.customer.stripePriceId !== undefined ? {
624
+ set: props.customer.stripePriceId
625
+ } : undefined,
626
+ stripeCurrentPeriodEnd: props.customer.stripeCurrentPeriodEnd !== undefined ? {
627
+ set: props.customer.stripeCurrentPeriodEnd
628
+ } : undefined,
629
+ },
630
+ create: {
631
+ authUserId: props.customer.authUserId !== undefined ? props.customer.authUserId : undefined,
632
+ name: props.customer.name !== undefined ? props.customer.name : undefined,
633
+ plan: props.customer.plan !== undefined ? props.customer.plan : undefined,
634
+ stripeCustomerId: props.customer.stripeCustomerId !== undefined ? props.customer.stripeCustomerId : undefined,
635
+ stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? props.customer.stripeSubscriptionId : undefined,
636
+ stripePriceId: props.customer.stripePriceId !== undefined ? props.customer.stripePriceId : undefined,
637
+ stripeCurrentPeriodEnd: props.customer.stripeCurrentPeriodEnd !== undefined ? props.customer.stripeCurrentPeriodEnd : undefined,
638
+ },
639
+ }
640
+ } : undefined,
641
+ accounts: props.accounts ?
642
+ Array.isArray(props.accounts) && props.accounts.length > 0 && props.accounts.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
643
+ connect: props.accounts.map((item) => ({
644
+ id: item.id
645
+ }))
646
+ } : { upsert: props.accounts.map((item) => ({
647
+ where: {
648
+ id: item.id !== undefined ? item.id : undefined,
649
+ providerAccountId: item.providerAccountId !== undefined ? item.providerAccountId : undefined,
650
+ userId: item.userId !== undefined ? {
651
+ equals: item.userId
652
+ } : undefined,
653
+ },
654
+ update: {
655
+ id: item.id !== undefined ? {
656
+ set: item.id
657
+ } : undefined,
658
+ type: item.type !== undefined ? {
659
+ set: item.type
660
+ } : undefined,
661
+ provider: item.provider !== undefined ? {
662
+ set: item.provider
663
+ } : undefined,
664
+ providerAccountId: item.providerAccountId !== undefined ? {
665
+ set: item.providerAccountId
666
+ } : undefined,
667
+ refresh_token: item.refresh_token !== undefined ? {
668
+ set: item.refresh_token
669
+ } : undefined,
670
+ access_token: item.access_token !== undefined ? {
671
+ set: item.access_token
672
+ } : undefined,
673
+ expires_at: item.expires_at !== undefined ? {
674
+ set: item.expires_at
675
+ } : undefined,
676
+ token_type: item.token_type !== undefined ? {
677
+ set: item.token_type
678
+ } : undefined,
679
+ scope: item.scope !== undefined ? {
680
+ set: item.scope
681
+ } : undefined,
682
+ id_token: item.id_token !== undefined ? {
683
+ set: item.id_token
684
+ } : undefined,
685
+ session_state: item.session_state !== undefined ? {
686
+ set: item.session_state
687
+ } : undefined,
688
+ },
689
+ create: {
690
+ type: item.type !== undefined ? item.type : undefined,
691
+ provider: item.provider !== undefined ? item.provider : undefined,
692
+ providerAccountId: item.providerAccountId !== undefined ? item.providerAccountId : undefined,
693
+ refresh_token: item.refresh_token !== undefined ? item.refresh_token : undefined,
694
+ access_token: item.access_token !== undefined ? item.access_token : undefined,
695
+ expires_at: item.expires_at !== undefined ? item.expires_at : undefined,
696
+ token_type: item.token_type !== undefined ? item.token_type : undefined,
697
+ scope: item.scope !== undefined ? item.scope : undefined,
698
+ id_token: item.id_token !== undefined ? item.id_token : undefined,
699
+ session_state: item.session_state !== undefined ? item.session_state : undefined,
700
+ },
701
+ }))
702
+ } : undefined,
703
+ sessions: props.sessions ?
704
+ Array.isArray(props.sessions) && props.sessions.length > 0 && props.sessions.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
705
+ connect: props.sessions.map((item) => ({
706
+ id: item.id
707
+ }))
708
+ } : { upsert: props.sessions.map((item) => ({
709
+ where: {
710
+ id: item.id !== undefined ? item.id : undefined,
711
+ userId: item.userId !== undefined ? {
712
+ equals: item.userId
713
+ } : undefined,
714
+ },
715
+ update: {
716
+ id: item.id !== undefined ? {
717
+ set: item.id
718
+ } : undefined,
719
+ sessionToken: item.sessionToken !== undefined ? {
720
+ set: item.sessionToken
721
+ } : undefined,
722
+ expires: item.expires !== undefined ? {
723
+ set: item.expires
724
+ } : undefined,
725
+ },
726
+ create: {
727
+ sessionToken: item.sessionToken !== undefined ? item.sessionToken : undefined,
728
+ expires: item.expires !== undefined ? item.expires : undefined,
729
+ },
730
+ }))
731
+ } : undefined,
732
+ authenticators: props.authenticators ?
733
+ Array.isArray(props.authenticators) && props.authenticators.length > 0 && props.authenticators.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
734
+ connect: props.authenticators.map((item) => ({
735
+ id: item.id
736
+ }))
737
+ } : { upsert: props.authenticators.map((item) => ({
738
+ where: {
739
+ id: item.id !== undefined ? item.id : undefined,
740
+ userId: item.userId !== undefined ? {
741
+ equals: item.userId
742
+ } : undefined,
743
+ },
744
+ update: {
745
+ id: item.id !== undefined ? {
746
+ set: item.id
747
+ } : undefined,
748
+ credentialID: item.credentialID !== undefined ? {
749
+ set: item.credentialID
750
+ } : undefined,
751
+ publicKey: item.publicKey !== undefined ? {
752
+ set: item.publicKey
753
+ } : undefined,
754
+ counter: item.counter !== undefined ? {
755
+ set: item.counter
756
+ } : undefined,
757
+ },
758
+ create: {
759
+ credentialID: item.credentialID !== undefined ? item.credentialID : undefined,
760
+ publicKey: item.publicKey !== undefined ? item.publicKey : undefined,
761
+ counter: item.counter !== undefined ? item.counter : undefined,
762
+ },
763
+ }))
764
+ } : undefined,
765
+ alpacaAccounts: props.alpacaAccounts ?
766
+ Array.isArray(props.alpacaAccounts) && props.alpacaAccounts.length > 0 && props.alpacaAccounts.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
767
+ connect: props.alpacaAccounts.map((item) => ({
768
+ id: item.id
769
+ }))
770
+ } : { upsert: props.alpacaAccounts.map((item) => ({
771
+ where: {
772
+ id: item.id !== undefined ? item.id : undefined,
773
+ userId: item.userId !== undefined ? {
774
+ equals: item.userId
775
+ } : undefined,
776
+ },
777
+ update: {
778
+ id: item.id !== undefined ? {
779
+ set: item.id
780
+ } : undefined,
781
+ type: item.type !== undefined ? {
782
+ set: item.type
783
+ } : undefined,
784
+ APIKey: item.APIKey !== undefined ? {
785
+ set: item.APIKey
786
+ } : undefined,
787
+ APISecret: item.APISecret !== undefined ? {
788
+ set: item.APISecret
789
+ } : undefined,
790
+ configuration: item.configuration !== undefined ? {
791
+ set: item.configuration
792
+ } : undefined,
793
+ marketOpen: item.marketOpen !== undefined ? {
794
+ set: item.marketOpen
795
+ } : undefined,
796
+ realTime: item.realTime !== undefined ? {
797
+ set: item.realTime
798
+ } : undefined,
799
+ cryptoTradingEnabled: item.cryptoTradingEnabled !== undefined ? {
800
+ set: item.cryptoTradingEnabled
801
+ } : undefined,
802
+ cryptoTradingPairs: item.cryptoTradingPairs !== undefined ? {
803
+ set: item.cryptoTradingPairs
804
+ } : undefined,
805
+ cryptoTradeAllocationPct: item.cryptoTradeAllocationPct !== undefined ? {
806
+ set: item.cryptoTradeAllocationPct
807
+ } : undefined,
808
+ tradeAllocationPct: item.tradeAllocationPct !== undefined ? {
809
+ set: item.tradeAllocationPct
810
+ } : undefined,
811
+ minPercentageChange: item.minPercentageChange !== undefined ? {
812
+ set: item.minPercentageChange
813
+ } : undefined,
814
+ volumeThreshold: item.volumeThreshold !== undefined ? {
815
+ set: item.volumeThreshold
816
+ } : undefined,
817
+ enablePortfolioTrailingStop: item.enablePortfolioTrailingStop !== undefined ? {
818
+ set: item.enablePortfolioTrailingStop
819
+ } : undefined,
820
+ portfolioTrailPercent: item.portfolioTrailPercent !== undefined ? {
821
+ set: item.portfolioTrailPercent
822
+ } : undefined,
823
+ portfolioProfitThresholdPercent: item.portfolioProfitThresholdPercent !== undefined ? {
824
+ set: item.portfolioProfitThresholdPercent
825
+ } : undefined,
826
+ reducedPortfolioTrailPercent: item.reducedPortfolioTrailPercent !== undefined ? {
827
+ set: item.reducedPortfolioTrailPercent
828
+ } : undefined,
829
+ defaultTrailingStopPercentage100: item.defaultTrailingStopPercentage100 !== undefined ? {
830
+ set: item.defaultTrailingStopPercentage100
831
+ } : undefined,
832
+ firstTrailReductionThreshold100: item.firstTrailReductionThreshold100 !== undefined ? {
833
+ set: item.firstTrailReductionThreshold100
834
+ } : undefined,
835
+ secondTrailReductionThreshold100: item.secondTrailReductionThreshold100 !== undefined ? {
836
+ set: item.secondTrailReductionThreshold100
837
+ } : undefined,
838
+ firstReducedTrailPercentage100: item.firstReducedTrailPercentage100 !== undefined ? {
839
+ set: item.firstReducedTrailPercentage100
840
+ } : undefined,
841
+ secondReducedTrailPercentage100: item.secondReducedTrailPercentage100 !== undefined ? {
842
+ set: item.secondReducedTrailPercentage100
843
+ } : undefined,
844
+ minimumPriceChangePercent100: item.minimumPriceChangePercent100 !== undefined ? {
845
+ set: item.minimumPriceChangePercent100
846
+ } : undefined,
847
+ allocation: item.allocation ?
848
+ typeof item.allocation === 'object' && Object.keys(item.allocation).length === 1 && (Object.keys(item.allocation)[0] === 'id' || Object.keys(item.allocation)[0] === 'symbol')
849
+ ? {
850
+ connect: {
851
+ id: item.allocation.id
852
+ }
853
+ } : { upsert: {
300
854
  where: {
301
- id: item.allocation.id !== undefined ? item.allocation.id : undefined,
302
- alpacaAccountId: item.allocation.alpacaAccountId !== undefined ? item.allocation.alpacaAccountId : undefined,
855
+ id: item.allocation.id !== undefined ? {
856
+ equals: item.allocation.id
857
+ } : undefined,
858
+ alpacaAccountId: item.allocation.alpacaAccountId !== undefined ? {
859
+ equals: item.allocation.alpacaAccountId
860
+ } : undefined,
861
+ },
862
+ update: {
863
+ id: item.allocation.id !== undefined ? {
864
+ set: item.allocation.id
865
+ } : undefined,
866
+ stocks: item.allocation.stocks !== undefined ? {
867
+ set: item.allocation.stocks
868
+ } : undefined,
869
+ crypto: item.allocation.crypto !== undefined ? {
870
+ set: item.allocation.crypto
871
+ } : undefined,
872
+ etfs: item.allocation.etfs !== undefined ? {
873
+ set: item.allocation.etfs
874
+ } : undefined,
303
875
  },
304
876
  create: {
305
877
  stocks: item.allocation.stocks !== undefined ? item.allocation.stocks : undefined,
@@ -308,19 +880,32 @@ export const User = {
308
880
  },
309
881
  }
310
882
  } : undefined,
311
- alerts: item.alerts ?
312
- Array.isArray(item.alerts) && item.alerts.length > 0 && item.alerts.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
313
- connect: item.alerts.map((item) => ({
314
- id: item.id
315
- }))
316
- }
317
- : { connectOrCreate: item.alerts.map((item) => ({
883
+ alerts: item.alerts ?
884
+ Array.isArray(item.alerts) && item.alerts.length > 0 && item.alerts.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
885
+ connect: item.alerts.map((item) => ({
886
+ id: item.id
887
+ }))
888
+ } : { upsert: item.alerts.map((item) => ({
318
889
  where: {
319
890
  id: item.id !== undefined ? item.id : undefined,
320
891
  alpacaAccountId: item.alpacaAccountId !== undefined ? {
321
892
  equals: item.alpacaAccountId
322
893
  } : undefined,
323
894
  },
895
+ update: {
896
+ id: item.id !== undefined ? {
897
+ set: item.id
898
+ } : undefined,
899
+ message: item.message !== undefined ? {
900
+ set: item.message
901
+ } : undefined,
902
+ type: item.type !== undefined ? {
903
+ set: item.type
904
+ } : undefined,
905
+ isRead: item.isRead !== undefined ? {
906
+ set: item.isRead
907
+ } : undefined,
908
+ },
324
909
  create: {
325
910
  message: item.message !== undefined ? item.message : undefined,
326
911
  type: item.type !== undefined ? item.type : undefined,
@@ -328,765 +913,1255 @@ export const User = {
328
913
  },
329
914
  }))
330
915
  } : undefined,
331
- },
332
- }))
333
- } : undefined,
334
- },
335
- };
336
- const filteredVariables = removeUndefinedProps(variables);
337
- try {
338
- const response = await client.mutate({ mutation: CREATE_ONE_USER, variables: filteredVariables });
339
- if (response.errors && response.errors.length > 0)
340
- throw new Error(response.errors[0].message);
341
- if (response && response.data && response.data.createOneUser) {
342
- return response.data.createOneUser;
916
+ },
917
+ create: {
918
+ type: item.type !== undefined ? item.type : undefined,
919
+ APIKey: item.APIKey !== undefined ? item.APIKey : undefined,
920
+ APISecret: item.APISecret !== undefined ? item.APISecret : undefined,
921
+ configuration: item.configuration !== undefined ? item.configuration : undefined,
922
+ marketOpen: item.marketOpen !== undefined ? item.marketOpen : undefined,
923
+ realTime: item.realTime !== undefined ? item.realTime : undefined,
924
+ cryptoTradingEnabled: item.cryptoTradingEnabled !== undefined ? item.cryptoTradingEnabled : undefined,
925
+ cryptoTradingPairs: item.cryptoTradingPairs !== undefined ? {
926
+ set: item.cryptoTradingPairs
927
+ } : undefined,
928
+ cryptoTradeAllocationPct: item.cryptoTradeAllocationPct !== undefined ? item.cryptoTradeAllocationPct : undefined,
929
+ tradeAllocationPct: item.tradeAllocationPct !== undefined ? item.tradeAllocationPct : undefined,
930
+ minPercentageChange: item.minPercentageChange !== undefined ? item.minPercentageChange : undefined,
931
+ volumeThreshold: item.volumeThreshold !== undefined ? item.volumeThreshold : undefined,
932
+ enablePortfolioTrailingStop: item.enablePortfolioTrailingStop !== undefined ? item.enablePortfolioTrailingStop : undefined,
933
+ portfolioTrailPercent: item.portfolioTrailPercent !== undefined ? item.portfolioTrailPercent : undefined,
934
+ portfolioProfitThresholdPercent: item.portfolioProfitThresholdPercent !== undefined ? item.portfolioProfitThresholdPercent : undefined,
935
+ reducedPortfolioTrailPercent: item.reducedPortfolioTrailPercent !== undefined ? item.reducedPortfolioTrailPercent : undefined,
936
+ defaultTrailingStopPercentage100: item.defaultTrailingStopPercentage100 !== undefined ? item.defaultTrailingStopPercentage100 : undefined,
937
+ firstTrailReductionThreshold100: item.firstTrailReductionThreshold100 !== undefined ? item.firstTrailReductionThreshold100 : undefined,
938
+ secondTrailReductionThreshold100: item.secondTrailReductionThreshold100 !== undefined ? item.secondTrailReductionThreshold100 : undefined,
939
+ firstReducedTrailPercentage100: item.firstReducedTrailPercentage100 !== undefined ? item.firstReducedTrailPercentage100 : undefined,
940
+ secondReducedTrailPercentage100: item.secondReducedTrailPercentage100 !== undefined ? item.secondReducedTrailPercentage100 : undefined,
941
+ minimumPriceChangePercent100: item.minimumPriceChangePercent100 !== undefined ? item.minimumPriceChangePercent100 : undefined,
942
+ allocation: item.allocation ?
943
+ typeof item.allocation === 'object' && Object.keys(item.allocation).length === 1 && Object.keys(item.allocation)[0] === 'id'
944
+ ? { connect: {
945
+ id: item.allocation.id
946
+ }
947
+ }
948
+ : { connectOrCreate: {
949
+ where: {
950
+ id: item.allocation.id !== undefined ? item.allocation.id : undefined,
951
+ alpacaAccountId: item.allocation.alpacaAccountId !== undefined ? item.allocation.alpacaAccountId : undefined,
952
+ },
953
+ create: {
954
+ stocks: item.allocation.stocks !== undefined ? item.allocation.stocks : undefined,
955
+ crypto: item.allocation.crypto !== undefined ? item.allocation.crypto : undefined,
956
+ etfs: item.allocation.etfs !== undefined ? item.allocation.etfs : undefined,
957
+ },
958
+ }
959
+ } : undefined,
960
+ alerts: item.alerts ?
961
+ Array.isArray(item.alerts) && item.alerts.length > 0 && item.alerts.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
962
+ connect: item.alerts.map((item) => ({
963
+ id: item.id
964
+ }))
965
+ }
966
+ : { connectOrCreate: item.alerts.map((item) => ({
967
+ where: {
968
+ id: item.id !== undefined ? item.id : undefined,
969
+ alpacaAccountId: item.alpacaAccountId !== undefined ? {
970
+ equals: item.alpacaAccountId
971
+ } : undefined,
972
+ },
973
+ create: {
974
+ message: item.message !== undefined ? item.message : undefined,
975
+ type: item.type !== undefined ? item.type : undefined,
976
+ isRead: item.isRead !== undefined ? item.isRead : undefined,
977
+ },
978
+ }))
979
+ } : undefined,
980
+ },
981
+ }))
982
+ } : undefined,
983
+ },
984
+ };
985
+ const filteredVariables = removeUndefinedProps(variables);
986
+ const response = await client.mutate({
987
+ mutation: UPDATE_ONE_USER,
988
+ variables: filteredVariables,
989
+ // Don't cache mutations, but ensure we're using the freshest context
990
+ fetchPolicy: 'no-cache'
991
+ });
992
+ if (response.errors && response.errors.length > 0)
993
+ throw new Error(response.errors[0].message);
994
+ if (response && response.data && response.data.updateOneUser) {
995
+ return response.data.updateOneUser;
996
+ }
997
+ else {
998
+ return null;
999
+ }
343
1000
  }
344
- else {
345
- return null;
1001
+ catch (error) {
1002
+ lastError = error;
1003
+ // Check if this is a database connection error that we should retry
1004
+ const isConnectionError = error.message?.includes('Server has closed the connection') ||
1005
+ error.message?.includes('Cannot reach database server') ||
1006
+ error.message?.includes('Connection timed out') ||
1007
+ error.message?.includes('Accelerate') || // Prisma Accelerate proxy errors
1008
+ (error.networkError && error.networkError.message?.includes('Failed to fetch'));
1009
+ if (isConnectionError && retryCount < MAX_RETRIES - 1) {
1010
+ retryCount++;
1011
+ const delay = Math.pow(2, retryCount) * 100; // Exponential backoff: 200ms, 400ms, 800ms
1012
+ console.warn("Database connection error, retrying...");
1013
+ await new Promise(resolve => setTimeout(resolve, delay));
1014
+ continue;
1015
+ }
1016
+ // Log the error and rethrow
1017
+ console.error("Database error occurred:", error);
1018
+ throw error;
346
1019
  }
347
1020
  }
348
- catch (error) {
349
- console.error('Error in createOneUser:', error);
350
- throw error;
351
- }
1021
+ // If we exhausted retries, throw the last error
1022
+ throw lastError;
352
1023
  },
353
1024
  /**
354
- * Create multiple User records.
355
- * @param props - Array of User objects for the new records.
1025
+ * Upsert a single User record.
1026
+ * Enhanced with connection resilience against Prisma connection errors.
1027
+ * @param props - Properties to update.
356
1028
  * @param globalClient - Apollo Client instance.
357
- * @returns The count of created records or null.
1029
+ * @returns The updated User or null.
358
1030
  */
359
- async createMany(props, globalClient) {
360
- const [modules, client] = await Promise.all([
361
- getApolloModules(),
362
- globalClient
363
- ? Promise.resolve(globalClient)
364
- : importedClient
365
- ]);
366
- const { gql, ApolloError } = modules;
367
- const CREATE_MANY_USER = gql `
368
- mutation createManyUser($data: [UserCreateManyInput!]!) {
369
- createManyUser(data: $data) {
370
- count
371
- }
372
- }`;
373
- const variables = {
374
- data: props.map(prop => ({
375
- name: prop.name !== undefined ? prop.name : undefined,
376
- email: prop.email !== undefined ? prop.email : undefined,
377
- emailVerified: prop.emailVerified !== undefined ? prop.emailVerified : undefined,
378
- image: prop.image !== undefined ? prop.image : undefined,
379
- role: prop.role !== undefined ? prop.role : undefined,
380
- bio: prop.bio !== undefined ? prop.bio : undefined,
381
- jobTitle: prop.jobTitle !== undefined ? prop.jobTitle : undefined,
382
- currentAccount: prop.currentAccount !== undefined ? prop.currentAccount : undefined,
383
- customerId: prop.customerId !== undefined ? prop.customerId : undefined,
384
- plan: prop.plan !== undefined ? prop.plan : undefined,
385
- openaiAPIKey: prop.openaiAPIKey !== undefined ? prop.openaiAPIKey : undefined,
386
- openaiModel: prop.openaiModel !== undefined ? prop.openaiModel : undefined,
387
- })),
388
- };
389
- const filteredVariables = removeUndefinedProps(variables);
390
- try {
391
- const response = await client.mutate({ mutation: CREATE_MANY_USER, variables: filteredVariables });
392
- if (response.errors && response.errors.length > 0)
393
- throw new Error(response.errors[0].message);
394
- if (response && response.data && response.data.createManyUser) {
395
- return response.data.createManyUser;
1031
+ async upsert(props, globalClient) {
1032
+ // Maximum number of retries for database connection issues
1033
+ const MAX_RETRIES = 3;
1034
+ let retryCount = 0;
1035
+ let lastError = null;
1036
+ // Retry loop to handle potential database connection issues
1037
+ while (retryCount < MAX_RETRIES) {
1038
+ try {
1039
+ const [modules, client] = await Promise.all([
1040
+ getApolloModules(),
1041
+ globalClient
1042
+ ? Promise.resolve(globalClient)
1043
+ : importedClient
1044
+ ]);
1045
+ const { gql, ApolloError } = modules;
1046
+ const UPSERT_ONE_USER = gql `
1047
+ mutation upsertOneUser($where: UserWhereUniqueInput!, $create: UserCreateInput!, $update: UserUpdateInput!) {
1048
+ upsertOneUser(where: $where, create: $create, update: $update) {
1049
+ ${selectionSet}
396
1050
  }
397
- else {
398
- return null;
1051
+ }`;
1052
+ const variables = {
1053
+ where: {
1054
+ id: props.id !== undefined ? props.id : undefined,
1055
+ email: props.email !== undefined ? props.email : undefined,
1056
+ name: props.name !== undefined ? props.name : undefined,
1057
+ emailVerified: props.emailVerified !== undefined ? {
1058
+ equals: props.emailVerified
1059
+ } : undefined,
1060
+ image: props.image !== undefined ? {
1061
+ equals: props.image
1062
+ } : undefined,
1063
+ createdAt: props.createdAt !== undefined ? {
1064
+ equals: props.createdAt
1065
+ } : undefined,
1066
+ updatedAt: props.updatedAt !== undefined ? {
1067
+ equals: props.updatedAt
1068
+ } : undefined,
1069
+ role: props.role !== undefined ? {
1070
+ equals: props.role
1071
+ } : undefined,
1072
+ bio: props.bio !== undefined ? {
1073
+ equals: props.bio
1074
+ } : undefined,
1075
+ jobTitle: props.jobTitle !== undefined ? {
1076
+ equals: props.jobTitle
1077
+ } : undefined,
1078
+ currentAccount: props.currentAccount !== undefined ? {
1079
+ equals: props.currentAccount
1080
+ } : undefined,
1081
+ plan: props.plan !== undefined ? {
1082
+ equals: props.plan
1083
+ } : undefined,
1084
+ openaiAPIKey: props.openaiAPIKey !== undefined ? {
1085
+ equals: props.openaiAPIKey
1086
+ } : undefined,
1087
+ openaiModel: props.openaiModel !== undefined ? {
1088
+ equals: props.openaiModel
1089
+ } : undefined,
1090
+ },
1091
+ create: {
1092
+ name: props.name !== undefined ? props.name : undefined,
1093
+ email: props.email !== undefined ? props.email : undefined,
1094
+ emailVerified: props.emailVerified !== undefined ? props.emailVerified : undefined,
1095
+ image: props.image !== undefined ? props.image : undefined,
1096
+ role: props.role !== undefined ? props.role : undefined,
1097
+ bio: props.bio !== undefined ? props.bio : undefined,
1098
+ jobTitle: props.jobTitle !== undefined ? props.jobTitle : undefined,
1099
+ currentAccount: props.currentAccount !== undefined ? props.currentAccount : undefined,
1100
+ plan: props.plan !== undefined ? props.plan : undefined,
1101
+ openaiAPIKey: props.openaiAPIKey !== undefined ? props.openaiAPIKey : undefined,
1102
+ openaiModel: props.openaiModel !== undefined ? props.openaiModel : undefined,
1103
+ customer: props.customer ?
1104
+ typeof props.customer === 'object' && Object.keys(props.customer).length === 1 && Object.keys(props.customer)[0] === 'id'
1105
+ ? { connect: {
1106
+ id: props.customer.id
1107
+ }
1108
+ }
1109
+ : { connectOrCreate: {
1110
+ where: {
1111
+ id: props.customer.id !== undefined ? props.customer.id : undefined,
1112
+ stripeCustomerId: props.customer.stripeCustomerId !== undefined ? props.customer.stripeCustomerId : undefined,
1113
+ stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? props.customer.stripeSubscriptionId : undefined,
1114
+ authUserId: props.customer.authUserId !== undefined ? {
1115
+ equals: props.customer.authUserId
1116
+ } : undefined,
1117
+ name: props.customer.name !== undefined ? {
1118
+ equals: props.customer.name
1119
+ } : undefined,
1120
+ stripePriceId: props.customer.stripePriceId !== undefined ? {
1121
+ equals: props.customer.stripePriceId
1122
+ } : undefined,
1123
+ },
1124
+ create: {
1125
+ authUserId: props.customer.authUserId !== undefined ? props.customer.authUserId : undefined,
1126
+ name: props.customer.name !== undefined ? props.customer.name : undefined,
1127
+ plan: props.customer.plan !== undefined ? props.customer.plan : undefined,
1128
+ stripeCustomerId: props.customer.stripeCustomerId !== undefined ? props.customer.stripeCustomerId : undefined,
1129
+ stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? props.customer.stripeSubscriptionId : undefined,
1130
+ stripePriceId: props.customer.stripePriceId !== undefined ? props.customer.stripePriceId : undefined,
1131
+ stripeCurrentPeriodEnd: props.customer.stripeCurrentPeriodEnd !== undefined ? props.customer.stripeCurrentPeriodEnd : undefined,
1132
+ },
1133
+ }
1134
+ } : undefined,
1135
+ accounts: props.accounts ?
1136
+ Array.isArray(props.accounts) && props.accounts.length > 0 && props.accounts.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
1137
+ connect: props.accounts.map((item) => ({
1138
+ id: item.id
1139
+ }))
1140
+ }
1141
+ : { connectOrCreate: props.accounts.map((item) => ({
1142
+ where: {
1143
+ id: item.id !== undefined ? item.id : undefined,
1144
+ providerAccountId: item.providerAccountId !== undefined ? item.providerAccountId : undefined,
1145
+ userId: item.userId !== undefined ? {
1146
+ equals: item.userId
1147
+ } : undefined,
1148
+ },
1149
+ create: {
1150
+ type: item.type !== undefined ? item.type : undefined,
1151
+ provider: item.provider !== undefined ? item.provider : undefined,
1152
+ providerAccountId: item.providerAccountId !== undefined ? item.providerAccountId : undefined,
1153
+ refresh_token: item.refresh_token !== undefined ? item.refresh_token : undefined,
1154
+ access_token: item.access_token !== undefined ? item.access_token : undefined,
1155
+ expires_at: item.expires_at !== undefined ? item.expires_at : undefined,
1156
+ token_type: item.token_type !== undefined ? item.token_type : undefined,
1157
+ scope: item.scope !== undefined ? item.scope : undefined,
1158
+ id_token: item.id_token !== undefined ? item.id_token : undefined,
1159
+ session_state: item.session_state !== undefined ? item.session_state : undefined,
1160
+ },
1161
+ }))
1162
+ } : undefined,
1163
+ sessions: props.sessions ?
1164
+ Array.isArray(props.sessions) && props.sessions.length > 0 && props.sessions.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
1165
+ connect: props.sessions.map((item) => ({
1166
+ id: item.id
1167
+ }))
1168
+ }
1169
+ : { connectOrCreate: props.sessions.map((item) => ({
1170
+ where: {
1171
+ id: item.id !== undefined ? item.id : undefined,
1172
+ userId: item.userId !== undefined ? {
1173
+ equals: item.userId
1174
+ } : undefined,
1175
+ },
1176
+ create: {
1177
+ sessionToken: item.sessionToken !== undefined ? item.sessionToken : undefined,
1178
+ expires: item.expires !== undefined ? item.expires : undefined,
1179
+ },
1180
+ }))
1181
+ } : undefined,
1182
+ authenticators: props.authenticators ?
1183
+ Array.isArray(props.authenticators) && props.authenticators.length > 0 && props.authenticators.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
1184
+ connect: props.authenticators.map((item) => ({
1185
+ id: item.id
1186
+ }))
1187
+ }
1188
+ : { connectOrCreate: props.authenticators.map((item) => ({
1189
+ where: {
1190
+ id: item.id !== undefined ? item.id : undefined,
1191
+ userId: item.userId !== undefined ? {
1192
+ equals: item.userId
1193
+ } : undefined,
1194
+ },
1195
+ create: {
1196
+ credentialID: item.credentialID !== undefined ? item.credentialID : undefined,
1197
+ publicKey: item.publicKey !== undefined ? item.publicKey : undefined,
1198
+ counter: item.counter !== undefined ? item.counter : undefined,
1199
+ },
1200
+ }))
1201
+ } : undefined,
1202
+ alpacaAccounts: props.alpacaAccounts ?
1203
+ Array.isArray(props.alpacaAccounts) && props.alpacaAccounts.length > 0 && props.alpacaAccounts.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
1204
+ connect: props.alpacaAccounts.map((item) => ({
1205
+ id: item.id
1206
+ }))
1207
+ }
1208
+ : { connectOrCreate: props.alpacaAccounts.map((item) => ({
1209
+ where: {
1210
+ id: item.id !== undefined ? item.id : undefined,
1211
+ userId: item.userId !== undefined ? {
1212
+ equals: item.userId
1213
+ } : undefined,
1214
+ },
1215
+ create: {
1216
+ type: item.type !== undefined ? item.type : undefined,
1217
+ APIKey: item.APIKey !== undefined ? item.APIKey : undefined,
1218
+ APISecret: item.APISecret !== undefined ? item.APISecret : undefined,
1219
+ configuration: item.configuration !== undefined ? item.configuration : undefined,
1220
+ marketOpen: item.marketOpen !== undefined ? item.marketOpen : undefined,
1221
+ realTime: item.realTime !== undefined ? item.realTime : undefined,
1222
+ cryptoTradingEnabled: item.cryptoTradingEnabled !== undefined ? item.cryptoTradingEnabled : undefined,
1223
+ cryptoTradingPairs: item.cryptoTradingPairs !== undefined ? {
1224
+ set: item.cryptoTradingPairs
1225
+ } : undefined,
1226
+ cryptoTradeAllocationPct: item.cryptoTradeAllocationPct !== undefined ? item.cryptoTradeAllocationPct : undefined,
1227
+ tradeAllocationPct: item.tradeAllocationPct !== undefined ? item.tradeAllocationPct : undefined,
1228
+ minPercentageChange: item.minPercentageChange !== undefined ? item.minPercentageChange : undefined,
1229
+ volumeThreshold: item.volumeThreshold !== undefined ? item.volumeThreshold : undefined,
1230
+ enablePortfolioTrailingStop: item.enablePortfolioTrailingStop !== undefined ? item.enablePortfolioTrailingStop : undefined,
1231
+ portfolioTrailPercent: item.portfolioTrailPercent !== undefined ? item.portfolioTrailPercent : undefined,
1232
+ portfolioProfitThresholdPercent: item.portfolioProfitThresholdPercent !== undefined ? item.portfolioProfitThresholdPercent : undefined,
1233
+ reducedPortfolioTrailPercent: item.reducedPortfolioTrailPercent !== undefined ? item.reducedPortfolioTrailPercent : undefined,
1234
+ defaultTrailingStopPercentage100: item.defaultTrailingStopPercentage100 !== undefined ? item.defaultTrailingStopPercentage100 : undefined,
1235
+ firstTrailReductionThreshold100: item.firstTrailReductionThreshold100 !== undefined ? item.firstTrailReductionThreshold100 : undefined,
1236
+ secondTrailReductionThreshold100: item.secondTrailReductionThreshold100 !== undefined ? item.secondTrailReductionThreshold100 : undefined,
1237
+ firstReducedTrailPercentage100: item.firstReducedTrailPercentage100 !== undefined ? item.firstReducedTrailPercentage100 : undefined,
1238
+ secondReducedTrailPercentage100: item.secondReducedTrailPercentage100 !== undefined ? item.secondReducedTrailPercentage100 : undefined,
1239
+ minimumPriceChangePercent100: item.minimumPriceChangePercent100 !== undefined ? item.minimumPriceChangePercent100 : undefined,
1240
+ allocation: item.allocation ?
1241
+ typeof item.allocation === 'object' && Object.keys(item.allocation).length === 1 && Object.keys(item.allocation)[0] === 'id'
1242
+ ? { connect: {
1243
+ id: item.allocation.id
1244
+ }
1245
+ }
1246
+ : { connectOrCreate: {
1247
+ where: {
1248
+ id: item.allocation.id !== undefined ? item.allocation.id : undefined,
1249
+ alpacaAccountId: item.allocation.alpacaAccountId !== undefined ? item.allocation.alpacaAccountId : undefined,
1250
+ },
1251
+ create: {
1252
+ stocks: item.allocation.stocks !== undefined ? item.allocation.stocks : undefined,
1253
+ crypto: item.allocation.crypto !== undefined ? item.allocation.crypto : undefined,
1254
+ etfs: item.allocation.etfs !== undefined ? item.allocation.etfs : undefined,
1255
+ },
1256
+ }
1257
+ } : undefined,
1258
+ alerts: item.alerts ?
1259
+ Array.isArray(item.alerts) && item.alerts.length > 0 && item.alerts.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
1260
+ connect: item.alerts.map((item) => ({
1261
+ id: item.id
1262
+ }))
1263
+ }
1264
+ : { connectOrCreate: item.alerts.map((item) => ({
1265
+ where: {
1266
+ id: item.id !== undefined ? item.id : undefined,
1267
+ alpacaAccountId: item.alpacaAccountId !== undefined ? {
1268
+ equals: item.alpacaAccountId
1269
+ } : undefined,
1270
+ },
1271
+ create: {
1272
+ message: item.message !== undefined ? item.message : undefined,
1273
+ type: item.type !== undefined ? item.type : undefined,
1274
+ isRead: item.isRead !== undefined ? item.isRead : undefined,
1275
+ },
1276
+ }))
1277
+ } : undefined,
1278
+ },
1279
+ }))
1280
+ } : undefined,
1281
+ },
1282
+ update: {
1283
+ name: props.name !== undefined ? {
1284
+ set: props.name
1285
+ } : undefined,
1286
+ email: props.email !== undefined ? {
1287
+ set: props.email
1288
+ } : undefined,
1289
+ emailVerified: props.emailVerified !== undefined ? {
1290
+ set: props.emailVerified
1291
+ } : undefined,
1292
+ image: props.image !== undefined ? {
1293
+ set: props.image
1294
+ } : undefined,
1295
+ role: props.role !== undefined ? {
1296
+ set: props.role
1297
+ } : undefined,
1298
+ bio: props.bio !== undefined ? {
1299
+ set: props.bio
1300
+ } : undefined,
1301
+ jobTitle: props.jobTitle !== undefined ? {
1302
+ set: props.jobTitle
1303
+ } : undefined,
1304
+ currentAccount: props.currentAccount !== undefined ? {
1305
+ set: props.currentAccount
1306
+ } : undefined,
1307
+ plan: props.plan !== undefined ? {
1308
+ set: props.plan
1309
+ } : undefined,
1310
+ openaiAPIKey: props.openaiAPIKey !== undefined ? {
1311
+ set: props.openaiAPIKey
1312
+ } : undefined,
1313
+ openaiModel: props.openaiModel !== undefined ? {
1314
+ set: props.openaiModel
1315
+ } : undefined,
1316
+ customer: props.customer ?
1317
+ typeof props.customer === 'object' && Object.keys(props.customer).length === 1 && (Object.keys(props.customer)[0] === 'id' || Object.keys(props.customer)[0] === 'symbol')
1318
+ ? {
1319
+ connect: {
1320
+ id: props.customer.id
1321
+ }
1322
+ } : { upsert: {
1323
+ where: {
1324
+ id: props.customer.id !== undefined ? {
1325
+ equals: props.customer.id
1326
+ } : undefined,
1327
+ authUserId: props.customer.authUserId !== undefined ? {
1328
+ equals: props.customer.authUserId
1329
+ } : undefined,
1330
+ name: props.customer.name !== undefined ? {
1331
+ equals: props.customer.name
1332
+ } : undefined,
1333
+ stripeCustomerId: props.customer.stripeCustomerId !== undefined ? {
1334
+ equals: props.customer.stripeCustomerId
1335
+ } : undefined,
1336
+ stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? {
1337
+ equals: props.customer.stripeSubscriptionId
1338
+ } : undefined,
1339
+ stripePriceId: props.customer.stripePriceId !== undefined ? {
1340
+ equals: props.customer.stripePriceId
1341
+ } : undefined,
1342
+ },
1343
+ update: {
1344
+ authUserId: props.customer.authUserId !== undefined ? {
1345
+ set: props.customer.authUserId
1346
+ } : undefined,
1347
+ name: props.customer.name !== undefined ? {
1348
+ set: props.customer.name
1349
+ } : undefined,
1350
+ plan: props.customer.plan !== undefined ? {
1351
+ set: props.customer.plan
1352
+ } : undefined,
1353
+ stripeCustomerId: props.customer.stripeCustomerId !== undefined ? {
1354
+ set: props.customer.stripeCustomerId
1355
+ } : undefined,
1356
+ stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? {
1357
+ set: props.customer.stripeSubscriptionId
1358
+ } : undefined,
1359
+ stripePriceId: props.customer.stripePriceId !== undefined ? {
1360
+ set: props.customer.stripePriceId
1361
+ } : undefined,
1362
+ stripeCurrentPeriodEnd: props.customer.stripeCurrentPeriodEnd !== undefined ? {
1363
+ set: props.customer.stripeCurrentPeriodEnd
1364
+ } : undefined,
1365
+ },
1366
+ create: {
1367
+ authUserId: props.customer.authUserId !== undefined ? props.customer.authUserId : undefined,
1368
+ name: props.customer.name !== undefined ? props.customer.name : undefined,
1369
+ plan: props.customer.plan !== undefined ? props.customer.plan : undefined,
1370
+ stripeCustomerId: props.customer.stripeCustomerId !== undefined ? props.customer.stripeCustomerId : undefined,
1371
+ stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? props.customer.stripeSubscriptionId : undefined,
1372
+ stripePriceId: props.customer.stripePriceId !== undefined ? props.customer.stripePriceId : undefined,
1373
+ stripeCurrentPeriodEnd: props.customer.stripeCurrentPeriodEnd !== undefined ? props.customer.stripeCurrentPeriodEnd : undefined,
1374
+ },
1375
+ }
1376
+ } : undefined,
1377
+ accounts: props.accounts ?
1378
+ Array.isArray(props.accounts) && props.accounts.length > 0 && props.accounts.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
1379
+ connect: props.accounts.map((item) => ({
1380
+ id: item.id
1381
+ }))
1382
+ } : { upsert: props.accounts.map((item) => ({
1383
+ where: {
1384
+ id: item.id !== undefined ? item.id : undefined,
1385
+ providerAccountId: item.providerAccountId !== undefined ? item.providerAccountId : undefined,
1386
+ userId: item.userId !== undefined ? {
1387
+ equals: item.userId
1388
+ } : undefined,
1389
+ },
1390
+ update: {
1391
+ id: item.id !== undefined ? {
1392
+ set: item.id
1393
+ } : undefined,
1394
+ type: item.type !== undefined ? {
1395
+ set: item.type
1396
+ } : undefined,
1397
+ provider: item.provider !== undefined ? {
1398
+ set: item.provider
1399
+ } : undefined,
1400
+ providerAccountId: item.providerAccountId !== undefined ? {
1401
+ set: item.providerAccountId
1402
+ } : undefined,
1403
+ refresh_token: item.refresh_token !== undefined ? {
1404
+ set: item.refresh_token
1405
+ } : undefined,
1406
+ access_token: item.access_token !== undefined ? {
1407
+ set: item.access_token
1408
+ } : undefined,
1409
+ expires_at: item.expires_at !== undefined ? {
1410
+ set: item.expires_at
1411
+ } : undefined,
1412
+ token_type: item.token_type !== undefined ? {
1413
+ set: item.token_type
1414
+ } : undefined,
1415
+ scope: item.scope !== undefined ? {
1416
+ set: item.scope
1417
+ } : undefined,
1418
+ id_token: item.id_token !== undefined ? {
1419
+ set: item.id_token
1420
+ } : undefined,
1421
+ session_state: item.session_state !== undefined ? {
1422
+ set: item.session_state
1423
+ } : undefined,
1424
+ },
1425
+ create: {
1426
+ type: item.type !== undefined ? item.type : undefined,
1427
+ provider: item.provider !== undefined ? item.provider : undefined,
1428
+ providerAccountId: item.providerAccountId !== undefined ? item.providerAccountId : undefined,
1429
+ refresh_token: item.refresh_token !== undefined ? item.refresh_token : undefined,
1430
+ access_token: item.access_token !== undefined ? item.access_token : undefined,
1431
+ expires_at: item.expires_at !== undefined ? item.expires_at : undefined,
1432
+ token_type: item.token_type !== undefined ? item.token_type : undefined,
1433
+ scope: item.scope !== undefined ? item.scope : undefined,
1434
+ id_token: item.id_token !== undefined ? item.id_token : undefined,
1435
+ session_state: item.session_state !== undefined ? item.session_state : undefined,
1436
+ },
1437
+ }))
1438
+ } : undefined,
1439
+ sessions: props.sessions ?
1440
+ Array.isArray(props.sessions) && props.sessions.length > 0 && props.sessions.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
1441
+ connect: props.sessions.map((item) => ({
1442
+ id: item.id
1443
+ }))
1444
+ } : { upsert: props.sessions.map((item) => ({
1445
+ where: {
1446
+ id: item.id !== undefined ? item.id : undefined,
1447
+ userId: item.userId !== undefined ? {
1448
+ equals: item.userId
1449
+ } : undefined,
1450
+ },
1451
+ update: {
1452
+ id: item.id !== undefined ? {
1453
+ set: item.id
1454
+ } : undefined,
1455
+ sessionToken: item.sessionToken !== undefined ? {
1456
+ set: item.sessionToken
1457
+ } : undefined,
1458
+ expires: item.expires !== undefined ? {
1459
+ set: item.expires
1460
+ } : undefined,
1461
+ },
1462
+ create: {
1463
+ sessionToken: item.sessionToken !== undefined ? item.sessionToken : undefined,
1464
+ expires: item.expires !== undefined ? item.expires : undefined,
1465
+ },
1466
+ }))
1467
+ } : undefined,
1468
+ authenticators: props.authenticators ?
1469
+ Array.isArray(props.authenticators) && props.authenticators.length > 0 && props.authenticators.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
1470
+ connect: props.authenticators.map((item) => ({
1471
+ id: item.id
1472
+ }))
1473
+ } : { upsert: props.authenticators.map((item) => ({
1474
+ where: {
1475
+ id: item.id !== undefined ? item.id : undefined,
1476
+ userId: item.userId !== undefined ? {
1477
+ equals: item.userId
1478
+ } : undefined,
1479
+ },
1480
+ update: {
1481
+ id: item.id !== undefined ? {
1482
+ set: item.id
1483
+ } : undefined,
1484
+ credentialID: item.credentialID !== undefined ? {
1485
+ set: item.credentialID
1486
+ } : undefined,
1487
+ publicKey: item.publicKey !== undefined ? {
1488
+ set: item.publicKey
1489
+ } : undefined,
1490
+ counter: item.counter !== undefined ? {
1491
+ set: item.counter
1492
+ } : undefined,
1493
+ },
1494
+ create: {
1495
+ credentialID: item.credentialID !== undefined ? item.credentialID : undefined,
1496
+ publicKey: item.publicKey !== undefined ? item.publicKey : undefined,
1497
+ counter: item.counter !== undefined ? item.counter : undefined,
1498
+ },
1499
+ }))
1500
+ } : undefined,
1501
+ alpacaAccounts: props.alpacaAccounts ?
1502
+ Array.isArray(props.alpacaAccounts) && props.alpacaAccounts.length > 0 && props.alpacaAccounts.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
1503
+ connect: props.alpacaAccounts.map((item) => ({
1504
+ id: item.id
1505
+ }))
1506
+ } : { upsert: props.alpacaAccounts.map((item) => ({
1507
+ where: {
1508
+ id: item.id !== undefined ? item.id : undefined,
1509
+ userId: item.userId !== undefined ? {
1510
+ equals: item.userId
1511
+ } : undefined,
1512
+ },
1513
+ update: {
1514
+ id: item.id !== undefined ? {
1515
+ set: item.id
1516
+ } : undefined,
1517
+ type: item.type !== undefined ? {
1518
+ set: item.type
1519
+ } : undefined,
1520
+ APIKey: item.APIKey !== undefined ? {
1521
+ set: item.APIKey
1522
+ } : undefined,
1523
+ APISecret: item.APISecret !== undefined ? {
1524
+ set: item.APISecret
1525
+ } : undefined,
1526
+ configuration: item.configuration !== undefined ? {
1527
+ set: item.configuration
1528
+ } : undefined,
1529
+ marketOpen: item.marketOpen !== undefined ? {
1530
+ set: item.marketOpen
1531
+ } : undefined,
1532
+ realTime: item.realTime !== undefined ? {
1533
+ set: item.realTime
1534
+ } : undefined,
1535
+ cryptoTradingEnabled: item.cryptoTradingEnabled !== undefined ? {
1536
+ set: item.cryptoTradingEnabled
1537
+ } : undefined,
1538
+ cryptoTradingPairs: item.cryptoTradingPairs !== undefined ? {
1539
+ set: item.cryptoTradingPairs
1540
+ } : undefined,
1541
+ cryptoTradeAllocationPct: item.cryptoTradeAllocationPct !== undefined ? {
1542
+ set: item.cryptoTradeAllocationPct
1543
+ } : undefined,
1544
+ tradeAllocationPct: item.tradeAllocationPct !== undefined ? {
1545
+ set: item.tradeAllocationPct
1546
+ } : undefined,
1547
+ minPercentageChange: item.minPercentageChange !== undefined ? {
1548
+ set: item.minPercentageChange
1549
+ } : undefined,
1550
+ volumeThreshold: item.volumeThreshold !== undefined ? {
1551
+ set: item.volumeThreshold
1552
+ } : undefined,
1553
+ enablePortfolioTrailingStop: item.enablePortfolioTrailingStop !== undefined ? {
1554
+ set: item.enablePortfolioTrailingStop
1555
+ } : undefined,
1556
+ portfolioTrailPercent: item.portfolioTrailPercent !== undefined ? {
1557
+ set: item.portfolioTrailPercent
1558
+ } : undefined,
1559
+ portfolioProfitThresholdPercent: item.portfolioProfitThresholdPercent !== undefined ? {
1560
+ set: item.portfolioProfitThresholdPercent
1561
+ } : undefined,
1562
+ reducedPortfolioTrailPercent: item.reducedPortfolioTrailPercent !== undefined ? {
1563
+ set: item.reducedPortfolioTrailPercent
1564
+ } : undefined,
1565
+ defaultTrailingStopPercentage100: item.defaultTrailingStopPercentage100 !== undefined ? {
1566
+ set: item.defaultTrailingStopPercentage100
1567
+ } : undefined,
1568
+ firstTrailReductionThreshold100: item.firstTrailReductionThreshold100 !== undefined ? {
1569
+ set: item.firstTrailReductionThreshold100
1570
+ } : undefined,
1571
+ secondTrailReductionThreshold100: item.secondTrailReductionThreshold100 !== undefined ? {
1572
+ set: item.secondTrailReductionThreshold100
1573
+ } : undefined,
1574
+ firstReducedTrailPercentage100: item.firstReducedTrailPercentage100 !== undefined ? {
1575
+ set: item.firstReducedTrailPercentage100
1576
+ } : undefined,
1577
+ secondReducedTrailPercentage100: item.secondReducedTrailPercentage100 !== undefined ? {
1578
+ set: item.secondReducedTrailPercentage100
1579
+ } : undefined,
1580
+ minimumPriceChangePercent100: item.minimumPriceChangePercent100 !== undefined ? {
1581
+ set: item.minimumPriceChangePercent100
1582
+ } : undefined,
1583
+ allocation: item.allocation ?
1584
+ typeof item.allocation === 'object' && Object.keys(item.allocation).length === 1 && (Object.keys(item.allocation)[0] === 'id' || Object.keys(item.allocation)[0] === 'symbol')
1585
+ ? {
1586
+ connect: {
1587
+ id: item.allocation.id
1588
+ }
1589
+ } : { upsert: {
1590
+ where: {
1591
+ id: item.allocation.id !== undefined ? {
1592
+ equals: item.allocation.id
1593
+ } : undefined,
1594
+ alpacaAccountId: item.allocation.alpacaAccountId !== undefined ? {
1595
+ equals: item.allocation.alpacaAccountId
1596
+ } : undefined,
1597
+ },
1598
+ update: {
1599
+ id: item.allocation.id !== undefined ? {
1600
+ set: item.allocation.id
1601
+ } : undefined,
1602
+ stocks: item.allocation.stocks !== undefined ? {
1603
+ set: item.allocation.stocks
1604
+ } : undefined,
1605
+ crypto: item.allocation.crypto !== undefined ? {
1606
+ set: item.allocation.crypto
1607
+ } : undefined,
1608
+ etfs: item.allocation.etfs !== undefined ? {
1609
+ set: item.allocation.etfs
1610
+ } : undefined,
1611
+ },
1612
+ create: {
1613
+ stocks: item.allocation.stocks !== undefined ? item.allocation.stocks : undefined,
1614
+ crypto: item.allocation.crypto !== undefined ? item.allocation.crypto : undefined,
1615
+ etfs: item.allocation.etfs !== undefined ? item.allocation.etfs : undefined,
1616
+ },
1617
+ }
1618
+ } : undefined,
1619
+ alerts: item.alerts ?
1620
+ Array.isArray(item.alerts) && item.alerts.length > 0 && item.alerts.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
1621
+ connect: item.alerts.map((item) => ({
1622
+ id: item.id
1623
+ }))
1624
+ } : { upsert: item.alerts.map((item) => ({
1625
+ where: {
1626
+ id: item.id !== undefined ? item.id : undefined,
1627
+ alpacaAccountId: item.alpacaAccountId !== undefined ? {
1628
+ equals: item.alpacaAccountId
1629
+ } : undefined,
1630
+ },
1631
+ update: {
1632
+ id: item.id !== undefined ? {
1633
+ set: item.id
1634
+ } : undefined,
1635
+ message: item.message !== undefined ? {
1636
+ set: item.message
1637
+ } : undefined,
1638
+ type: item.type !== undefined ? {
1639
+ set: item.type
1640
+ } : undefined,
1641
+ isRead: item.isRead !== undefined ? {
1642
+ set: item.isRead
1643
+ } : undefined,
1644
+ },
1645
+ create: {
1646
+ message: item.message !== undefined ? item.message : undefined,
1647
+ type: item.type !== undefined ? item.type : undefined,
1648
+ isRead: item.isRead !== undefined ? item.isRead : undefined,
1649
+ },
1650
+ }))
1651
+ } : undefined,
1652
+ },
1653
+ create: {
1654
+ type: item.type !== undefined ? item.type : undefined,
1655
+ APIKey: item.APIKey !== undefined ? item.APIKey : undefined,
1656
+ APISecret: item.APISecret !== undefined ? item.APISecret : undefined,
1657
+ configuration: item.configuration !== undefined ? item.configuration : undefined,
1658
+ marketOpen: item.marketOpen !== undefined ? item.marketOpen : undefined,
1659
+ realTime: item.realTime !== undefined ? item.realTime : undefined,
1660
+ cryptoTradingEnabled: item.cryptoTradingEnabled !== undefined ? item.cryptoTradingEnabled : undefined,
1661
+ cryptoTradingPairs: item.cryptoTradingPairs !== undefined ? {
1662
+ set: item.cryptoTradingPairs
1663
+ } : undefined,
1664
+ cryptoTradeAllocationPct: item.cryptoTradeAllocationPct !== undefined ? item.cryptoTradeAllocationPct : undefined,
1665
+ tradeAllocationPct: item.tradeAllocationPct !== undefined ? item.tradeAllocationPct : undefined,
1666
+ minPercentageChange: item.minPercentageChange !== undefined ? item.minPercentageChange : undefined,
1667
+ volumeThreshold: item.volumeThreshold !== undefined ? item.volumeThreshold : undefined,
1668
+ enablePortfolioTrailingStop: item.enablePortfolioTrailingStop !== undefined ? item.enablePortfolioTrailingStop : undefined,
1669
+ portfolioTrailPercent: item.portfolioTrailPercent !== undefined ? item.portfolioTrailPercent : undefined,
1670
+ portfolioProfitThresholdPercent: item.portfolioProfitThresholdPercent !== undefined ? item.portfolioProfitThresholdPercent : undefined,
1671
+ reducedPortfolioTrailPercent: item.reducedPortfolioTrailPercent !== undefined ? item.reducedPortfolioTrailPercent : undefined,
1672
+ defaultTrailingStopPercentage100: item.defaultTrailingStopPercentage100 !== undefined ? item.defaultTrailingStopPercentage100 : undefined,
1673
+ firstTrailReductionThreshold100: item.firstTrailReductionThreshold100 !== undefined ? item.firstTrailReductionThreshold100 : undefined,
1674
+ secondTrailReductionThreshold100: item.secondTrailReductionThreshold100 !== undefined ? item.secondTrailReductionThreshold100 : undefined,
1675
+ firstReducedTrailPercentage100: item.firstReducedTrailPercentage100 !== undefined ? item.firstReducedTrailPercentage100 : undefined,
1676
+ secondReducedTrailPercentage100: item.secondReducedTrailPercentage100 !== undefined ? item.secondReducedTrailPercentage100 : undefined,
1677
+ minimumPriceChangePercent100: item.minimumPriceChangePercent100 !== undefined ? item.minimumPriceChangePercent100 : undefined,
1678
+ allocation: item.allocation ?
1679
+ typeof item.allocation === 'object' && Object.keys(item.allocation).length === 1 && Object.keys(item.allocation)[0] === 'id'
1680
+ ? { connect: {
1681
+ id: item.allocation.id
1682
+ }
1683
+ }
1684
+ : { connectOrCreate: {
1685
+ where: {
1686
+ id: item.allocation.id !== undefined ? item.allocation.id : undefined,
1687
+ alpacaAccountId: item.allocation.alpacaAccountId !== undefined ? item.allocation.alpacaAccountId : undefined,
1688
+ },
1689
+ create: {
1690
+ stocks: item.allocation.stocks !== undefined ? item.allocation.stocks : undefined,
1691
+ crypto: item.allocation.crypto !== undefined ? item.allocation.crypto : undefined,
1692
+ etfs: item.allocation.etfs !== undefined ? item.allocation.etfs : undefined,
1693
+ },
1694
+ }
1695
+ } : undefined,
1696
+ alerts: item.alerts ?
1697
+ Array.isArray(item.alerts) && item.alerts.length > 0 && item.alerts.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
1698
+ connect: item.alerts.map((item) => ({
1699
+ id: item.id
1700
+ }))
1701
+ }
1702
+ : { connectOrCreate: item.alerts.map((item) => ({
1703
+ where: {
1704
+ id: item.id !== undefined ? item.id : undefined,
1705
+ alpacaAccountId: item.alpacaAccountId !== undefined ? {
1706
+ equals: item.alpacaAccountId
1707
+ } : undefined,
1708
+ },
1709
+ create: {
1710
+ message: item.message !== undefined ? item.message : undefined,
1711
+ type: item.type !== undefined ? item.type : undefined,
1712
+ isRead: item.isRead !== undefined ? item.isRead : undefined,
1713
+ },
1714
+ }))
1715
+ } : undefined,
1716
+ },
1717
+ }))
1718
+ } : undefined,
1719
+ },
1720
+ };
1721
+ const filteredVariables = removeUndefinedProps(variables);
1722
+ const response = await client.mutate({
1723
+ mutation: UPSERT_ONE_USER,
1724
+ variables: filteredVariables,
1725
+ // Don't cache mutations, but ensure we're using the freshest context
1726
+ fetchPolicy: 'no-cache'
1727
+ });
1728
+ if (response.errors && response.errors.length > 0)
1729
+ throw new Error(response.errors[0].message);
1730
+ if (response && response.data && response.data.upsertOneUser) {
1731
+ return response.data.upsertOneUser;
1732
+ }
1733
+ else {
1734
+ return null;
1735
+ }
1736
+ }
1737
+ catch (error) {
1738
+ lastError = error;
1739
+ // Check if this is a database connection error that we should retry
1740
+ const isConnectionError = error.message?.includes('Server has closed the connection') ||
1741
+ error.message?.includes('Cannot reach database server') ||
1742
+ error.message?.includes('Connection timed out') ||
1743
+ error.message?.includes('Accelerate') || // Prisma Accelerate proxy errors
1744
+ (error.networkError && error.networkError.message?.includes('Failed to fetch'));
1745
+ if (isConnectionError && retryCount < MAX_RETRIES - 1) {
1746
+ retryCount++;
1747
+ const delay = Math.pow(2, retryCount) * 100; // Exponential backoff: 200ms, 400ms, 800ms
1748
+ console.warn("Database connection error, retrying...");
1749
+ await new Promise(resolve => setTimeout(resolve, delay));
1750
+ continue;
1751
+ }
1752
+ // Log the error and rethrow
1753
+ console.error("Database error occurred:", error);
1754
+ throw error;
399
1755
  }
400
1756
  }
401
- catch (error) {
402
- console.error('Error in createManyUser:', error);
403
- throw error;
404
- }
1757
+ // If we exhausted retries, throw the last error
1758
+ throw lastError;
405
1759
  },
406
1760
  /**
407
- * Update a single User record.
408
- * @param props - Properties to update.
1761
+ * Update multiple User records.
1762
+ * Enhanced with connection resilience against Prisma connection errors.
1763
+ * @param props - Array of User objects for the updated records.
409
1764
  * @param globalClient - Apollo Client instance.
410
- * @returns The updated User or null.
1765
+ * @returns The count of created records or null.
411
1766
  */
412
- async update(props, globalClient) {
413
- const [modules, client] = await Promise.all([
414
- getApolloModules(),
415
- globalClient
416
- ? Promise.resolve(globalClient)
417
- : importedClient
418
- ]);
419
- const { gql, ApolloError } = modules;
420
- const UPDATE_ONE_USER = gql `
421
- mutation updateOneUser($data: UserUpdateInput!, $where: UserWhereUniqueInput!) {
422
- updateOneUser(data: $data, where: $where) {
423
- ${selectionSet}
424
- }
425
- }`;
426
- const variables = {
427
- where: {
428
- id: props.id !== undefined ? props.id : undefined,
429
- email: props.email !== undefined ? props.email : undefined,
430
- name: props.name !== undefined ? {
431
- equals: props.name
432
- } : undefined,
433
- },
434
- data: {
435
- id: props.id !== undefined ? {
436
- set: props.id
437
- } : undefined,
438
- name: props.name !== undefined ? {
439
- set: props.name
440
- } : undefined,
441
- email: props.email !== undefined ? {
442
- set: props.email
443
- } : undefined,
444
- emailVerified: props.emailVerified !== undefined ? {
445
- set: props.emailVerified
446
- } : undefined,
447
- image: props.image !== undefined ? {
448
- set: props.image
449
- } : undefined,
450
- createdAt: props.createdAt !== undefined ? {
451
- set: props.createdAt
452
- } : undefined,
453
- updatedAt: props.updatedAt !== undefined ? {
454
- set: props.updatedAt
455
- } : undefined,
456
- role: props.role !== undefined ? {
457
- set: props.role
458
- } : undefined,
459
- bio: props.bio !== undefined ? {
460
- set: props.bio
461
- } : undefined,
462
- jobTitle: props.jobTitle !== undefined ? {
463
- set: props.jobTitle
464
- } : undefined,
465
- currentAccount: props.currentAccount !== undefined ? {
466
- set: props.currentAccount
467
- } : undefined,
468
- plan: props.plan !== undefined ? {
469
- set: props.plan
470
- } : undefined,
471
- openaiAPIKey: props.openaiAPIKey !== undefined ? {
472
- set: props.openaiAPIKey
473
- } : undefined,
474
- openaiModel: props.openaiModel !== undefined ? {
475
- set: props.openaiModel
476
- } : undefined,
477
- customer: props.customer ?
478
- typeof props.customer === 'object' && Object.keys(props.customer).length === 1 && (Object.keys(props.customer)[0] === 'id' || Object.keys(props.customer)[0] === 'symbol')
479
- ? {
480
- connect: {
481
- id: props.customer.id
482
- }
483
- } : { upsert: {
484
- where: {
485
- id: props.customer.id !== undefined ? {
486
- equals: props.customer.id
487
- } : undefined,
488
- authUserId: props.customer.authUserId !== undefined ? {
489
- equals: props.customer.authUserId
490
- } : undefined,
491
- name: props.customer.name !== undefined ? {
492
- equals: props.customer.name
493
- } : undefined,
494
- stripeCustomerId: props.customer.stripeCustomerId !== undefined ? {
495
- equals: props.customer.stripeCustomerId
496
- } : undefined,
497
- stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? {
498
- equals: props.customer.stripeSubscriptionId
499
- } : undefined,
500
- stripePriceId: props.customer.stripePriceId !== undefined ? {
501
- equals: props.customer.stripePriceId
502
- } : undefined,
503
- },
504
- update: {
505
- authUserId: props.customer.authUserId !== undefined ? {
506
- set: props.customer.authUserId
507
- } : undefined,
508
- name: props.customer.name !== undefined ? {
509
- set: props.customer.name
510
- } : undefined,
511
- plan: props.customer.plan !== undefined ? {
512
- set: props.customer.plan
513
- } : undefined,
514
- stripeCustomerId: props.customer.stripeCustomerId !== undefined ? {
515
- set: props.customer.stripeCustomerId
516
- } : undefined,
517
- stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? {
518
- set: props.customer.stripeSubscriptionId
519
- } : undefined,
520
- stripePriceId: props.customer.stripePriceId !== undefined ? {
521
- set: props.customer.stripePriceId
522
- } : undefined,
523
- stripeCurrentPeriodEnd: props.customer.stripeCurrentPeriodEnd !== undefined ? {
524
- set: props.customer.stripeCurrentPeriodEnd
525
- } : undefined,
526
- },
527
- create: {
528
- authUserId: props.customer.authUserId !== undefined ? props.customer.authUserId : undefined,
529
- name: props.customer.name !== undefined ? props.customer.name : undefined,
530
- plan: props.customer.plan !== undefined ? props.customer.plan : undefined,
531
- stripeCustomerId: props.customer.stripeCustomerId !== undefined ? props.customer.stripeCustomerId : undefined,
532
- stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? props.customer.stripeSubscriptionId : undefined,
533
- stripePriceId: props.customer.stripePriceId !== undefined ? props.customer.stripePriceId : undefined,
534
- stripeCurrentPeriodEnd: props.customer.stripeCurrentPeriodEnd !== undefined ? props.customer.stripeCurrentPeriodEnd : undefined,
535
- },
536
- }
537
- } : undefined,
538
- accounts: props.accounts ?
539
- Array.isArray(props.accounts) && props.accounts.length > 0 && props.accounts.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
540
- connect: props.accounts.map((item) => ({
541
- id: item.id
542
- }))
543
- } : { upsert: props.accounts.map((item) => ({
544
- where: {
545
- id: item.id !== undefined ? item.id : undefined,
546
- userId: item.userId !== undefined ? {
547
- equals: item.userId
548
- } : undefined,
549
- providerAccountId: item.providerAccountId !== undefined ? {
550
- equals: item.providerAccountId
551
- } : undefined,
552
- },
553
- update: {
554
- id: item.id !== undefined ? {
555
- set: item.id
556
- } : undefined,
557
- type: item.type !== undefined ? {
558
- set: item.type
559
- } : undefined,
560
- provider: item.provider !== undefined ? {
561
- set: item.provider
562
- } : undefined,
563
- providerAccountId: item.providerAccountId !== undefined ? {
564
- set: item.providerAccountId
565
- } : undefined,
566
- refresh_token: item.refresh_token !== undefined ? {
567
- set: item.refresh_token
568
- } : undefined,
569
- access_token: item.access_token !== undefined ? {
570
- set: item.access_token
571
- } : undefined,
572
- expires_at: item.expires_at !== undefined ? {
573
- set: item.expires_at
574
- } : undefined,
575
- token_type: item.token_type !== undefined ? {
576
- set: item.token_type
577
- } : undefined,
578
- scope: item.scope !== undefined ? {
579
- set: item.scope
580
- } : undefined,
581
- id_token: item.id_token !== undefined ? {
582
- set: item.id_token
583
- } : undefined,
584
- session_state: item.session_state !== undefined ? {
585
- set: item.session_state
586
- } : undefined,
587
- },
588
- create: {
589
- type: item.type !== undefined ? item.type : undefined,
590
- provider: item.provider !== undefined ? item.provider : undefined,
591
- providerAccountId: item.providerAccountId !== undefined ? item.providerAccountId : undefined,
592
- refresh_token: item.refresh_token !== undefined ? item.refresh_token : undefined,
593
- access_token: item.access_token !== undefined ? item.access_token : undefined,
594
- expires_at: item.expires_at !== undefined ? item.expires_at : undefined,
595
- token_type: item.token_type !== undefined ? item.token_type : undefined,
596
- scope: item.scope !== undefined ? item.scope : undefined,
597
- id_token: item.id_token !== undefined ? item.id_token : undefined,
598
- session_state: item.session_state !== undefined ? item.session_state : undefined,
599
- },
600
- }))
601
- } : undefined,
602
- sessions: props.sessions ?
603
- Array.isArray(props.sessions) && props.sessions.length > 0 && props.sessions.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
604
- connect: props.sessions.map((item) => ({
605
- id: item.id
606
- }))
607
- } : { upsert: props.sessions.map((item) => ({
608
- where: {
609
- id: item.id !== undefined ? item.id : undefined,
610
- userId: item.userId !== undefined ? {
611
- equals: item.userId
612
- } : undefined,
613
- },
614
- update: {
615
- id: item.id !== undefined ? {
616
- set: item.id
617
- } : undefined,
618
- sessionToken: item.sessionToken !== undefined ? {
619
- set: item.sessionToken
620
- } : undefined,
621
- expires: item.expires !== undefined ? {
622
- set: item.expires
623
- } : undefined,
624
- },
625
- create: {
626
- sessionToken: item.sessionToken !== undefined ? item.sessionToken : undefined,
627
- expires: item.expires !== undefined ? item.expires : undefined,
628
- },
629
- }))
630
- } : undefined,
631
- authenticators: props.authenticators ?
632
- Array.isArray(props.authenticators) && props.authenticators.length > 0 && props.authenticators.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
633
- connect: props.authenticators.map((item) => ({
634
- id: item.id
635
- }))
636
- } : { upsert: props.authenticators.map((item) => ({
637
- where: {
638
- id: item.id !== undefined ? item.id : undefined,
639
- userId: item.userId !== undefined ? {
640
- equals: item.userId
641
- } : undefined,
642
- },
643
- update: {
644
- id: item.id !== undefined ? {
645
- set: item.id
646
- } : undefined,
647
- credentialID: item.credentialID !== undefined ? {
648
- set: item.credentialID
649
- } : undefined,
650
- publicKey: item.publicKey !== undefined ? {
651
- set: item.publicKey
652
- } : undefined,
653
- counter: item.counter !== undefined ? {
654
- set: item.counter
655
- } : undefined,
656
- },
657
- create: {
658
- credentialID: item.credentialID !== undefined ? item.credentialID : undefined,
659
- publicKey: item.publicKey !== undefined ? item.publicKey : undefined,
660
- counter: item.counter !== undefined ? item.counter : undefined,
661
- },
662
- }))
663
- } : undefined,
664
- alpacaAccounts: props.alpacaAccounts ?
665
- Array.isArray(props.alpacaAccounts) && props.alpacaAccounts.length > 0 && props.alpacaAccounts.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
666
- connect: props.alpacaAccounts.map((item) => ({
667
- id: item.id
668
- }))
669
- } : { upsert: props.alpacaAccounts.map((item) => ({
670
- where: {
671
- id: item.id !== undefined ? item.id : undefined,
672
- userId: item.userId !== undefined ? {
673
- equals: item.userId
674
- } : undefined,
675
- },
676
- update: {
677
- id: item.id !== undefined ? {
678
- set: item.id
679
- } : undefined,
680
- type: item.type !== undefined ? {
681
- set: item.type
682
- } : undefined,
683
- APIKey: item.APIKey !== undefined ? {
684
- set: item.APIKey
685
- } : undefined,
686
- APISecret: item.APISecret !== undefined ? {
687
- set: item.APISecret
688
- } : undefined,
689
- configuration: item.configuration !== undefined ? {
690
- set: item.configuration
691
- } : undefined,
692
- marketOpen: item.marketOpen !== undefined ? {
693
- set: item.marketOpen
694
- } : undefined,
695
- realTime: item.realTime !== undefined ? {
696
- set: item.realTime
697
- } : undefined,
698
- cryptoTradingEnabled: item.cryptoTradingEnabled !== undefined ? {
699
- set: item.cryptoTradingEnabled
700
- } : undefined,
701
- cryptoTradingPairs: item.cryptoTradingPairs !== undefined ? {
702
- set: item.cryptoTradingPairs
703
- } : undefined,
704
- cryptoTradeAllocationPct: item.cryptoTradeAllocationPct !== undefined ? {
705
- set: item.cryptoTradeAllocationPct
706
- } : undefined,
707
- tradeAllocationPct: item.tradeAllocationPct !== undefined ? {
708
- set: item.tradeAllocationPct
709
- } : undefined,
710
- minPercentageChange: item.minPercentageChange !== undefined ? {
711
- set: item.minPercentageChange
712
- } : undefined,
713
- volumeThreshold: item.volumeThreshold !== undefined ? {
714
- set: item.volumeThreshold
715
- } : undefined,
716
- enablePortfolioTrailingStop: item.enablePortfolioTrailingStop !== undefined ? {
717
- set: item.enablePortfolioTrailingStop
718
- } : undefined,
719
- portfolioTrailPercent: item.portfolioTrailPercent !== undefined ? {
720
- set: item.portfolioTrailPercent
721
- } : undefined,
722
- portfolioProfitThresholdPercent: item.portfolioProfitThresholdPercent !== undefined ? {
723
- set: item.portfolioProfitThresholdPercent
724
- } : undefined,
725
- reducedPortfolioTrailPercent: item.reducedPortfolioTrailPercent !== undefined ? {
726
- set: item.reducedPortfolioTrailPercent
727
- } : undefined,
728
- defaultTrailingStopPercentage100: item.defaultTrailingStopPercentage100 !== undefined ? {
729
- set: item.defaultTrailingStopPercentage100
730
- } : undefined,
731
- firstTrailReductionThreshold100: item.firstTrailReductionThreshold100 !== undefined ? {
732
- set: item.firstTrailReductionThreshold100
733
- } : undefined,
734
- secondTrailReductionThreshold100: item.secondTrailReductionThreshold100 !== undefined ? {
735
- set: item.secondTrailReductionThreshold100
736
- } : undefined,
737
- firstReducedTrailPercentage100: item.firstReducedTrailPercentage100 !== undefined ? {
738
- set: item.firstReducedTrailPercentage100
739
- } : undefined,
740
- secondReducedTrailPercentage100: item.secondReducedTrailPercentage100 !== undefined ? {
741
- set: item.secondReducedTrailPercentage100
742
- } : undefined,
743
- minimumPriceChangePercent100: item.minimumPriceChangePercent100 !== undefined ? {
744
- set: item.minimumPriceChangePercent100
745
- } : undefined,
746
- allocation: item.allocation ?
747
- typeof item.allocation === 'object' && Object.keys(item.allocation).length === 1 && (Object.keys(item.allocation)[0] === 'id' || Object.keys(item.allocation)[0] === 'symbol')
748
- ? {
749
- connect: {
750
- id: item.allocation.id
751
- }
752
- } : { upsert: {
753
- where: {
754
- id: item.allocation.id !== undefined ? {
755
- equals: item.allocation.id
756
- } : undefined,
757
- alpacaAccountId: item.allocation.alpacaAccountId !== undefined ? {
758
- equals: item.allocation.alpacaAccountId
759
- } : undefined,
760
- },
761
- update: {
762
- id: item.allocation.id !== undefined ? {
763
- set: item.allocation.id
764
- } : undefined,
765
- stocks: item.allocation.stocks !== undefined ? {
766
- set: item.allocation.stocks
767
- } : undefined,
768
- crypto: item.allocation.crypto !== undefined ? {
769
- set: item.allocation.crypto
770
- } : undefined,
771
- etfs: item.allocation.etfs !== undefined ? {
772
- set: item.allocation.etfs
773
- } : undefined,
774
- },
775
- create: {
776
- stocks: item.allocation.stocks !== undefined ? item.allocation.stocks : undefined,
777
- crypto: item.allocation.crypto !== undefined ? item.allocation.crypto : undefined,
778
- etfs: item.allocation.etfs !== undefined ? item.allocation.etfs : undefined,
779
- },
780
- }
781
- } : undefined,
782
- alerts: item.alerts ?
783
- Array.isArray(item.alerts) && item.alerts.length > 0 && item.alerts.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
784
- connect: item.alerts.map((item) => ({
785
- id: item.id
786
- }))
787
- } : { upsert: item.alerts.map((item) => ({
788
- where: {
789
- id: item.id !== undefined ? item.id : undefined,
790
- alpacaAccountId: item.alpacaAccountId !== undefined ? {
791
- equals: item.alpacaAccountId
792
- } : undefined,
793
- },
794
- update: {
795
- id: item.id !== undefined ? {
796
- set: item.id
797
- } : undefined,
798
- message: item.message !== undefined ? {
799
- set: item.message
800
- } : undefined,
801
- type: item.type !== undefined ? {
802
- set: item.type
803
- } : undefined,
804
- isRead: item.isRead !== undefined ? {
805
- set: item.isRead
806
- } : undefined,
807
- },
808
- create: {
809
- message: item.message !== undefined ? item.message : undefined,
810
- type: item.type !== undefined ? item.type : undefined,
811
- isRead: item.isRead !== undefined ? item.isRead : undefined,
812
- },
813
- }))
814
- } : undefined,
815
- },
816
- create: {
817
- type: item.type !== undefined ? item.type : undefined,
818
- APIKey: item.APIKey !== undefined ? item.APIKey : undefined,
819
- APISecret: item.APISecret !== undefined ? item.APISecret : undefined,
820
- configuration: item.configuration !== undefined ? item.configuration : undefined,
821
- marketOpen: item.marketOpen !== undefined ? item.marketOpen : undefined,
822
- realTime: item.realTime !== undefined ? item.realTime : undefined,
823
- cryptoTradingEnabled: item.cryptoTradingEnabled !== undefined ? item.cryptoTradingEnabled : undefined,
824
- cryptoTradingPairs: item.cryptoTradingPairs !== undefined ? {
825
- set: item.cryptoTradingPairs
826
- } : undefined,
827
- cryptoTradeAllocationPct: item.cryptoTradeAllocationPct !== undefined ? item.cryptoTradeAllocationPct : undefined,
828
- tradeAllocationPct: item.tradeAllocationPct !== undefined ? item.tradeAllocationPct : undefined,
829
- minPercentageChange: item.minPercentageChange !== undefined ? item.minPercentageChange : undefined,
830
- volumeThreshold: item.volumeThreshold !== undefined ? item.volumeThreshold : undefined,
831
- enablePortfolioTrailingStop: item.enablePortfolioTrailingStop !== undefined ? item.enablePortfolioTrailingStop : undefined,
832
- portfolioTrailPercent: item.portfolioTrailPercent !== undefined ? item.portfolioTrailPercent : undefined,
833
- portfolioProfitThresholdPercent: item.portfolioProfitThresholdPercent !== undefined ? item.portfolioProfitThresholdPercent : undefined,
834
- reducedPortfolioTrailPercent: item.reducedPortfolioTrailPercent !== undefined ? item.reducedPortfolioTrailPercent : undefined,
835
- defaultTrailingStopPercentage100: item.defaultTrailingStopPercentage100 !== undefined ? item.defaultTrailingStopPercentage100 : undefined,
836
- firstTrailReductionThreshold100: item.firstTrailReductionThreshold100 !== undefined ? item.firstTrailReductionThreshold100 : undefined,
837
- secondTrailReductionThreshold100: item.secondTrailReductionThreshold100 !== undefined ? item.secondTrailReductionThreshold100 : undefined,
838
- firstReducedTrailPercentage100: item.firstReducedTrailPercentage100 !== undefined ? item.firstReducedTrailPercentage100 : undefined,
839
- secondReducedTrailPercentage100: item.secondReducedTrailPercentage100 !== undefined ? item.secondReducedTrailPercentage100 : undefined,
840
- minimumPriceChangePercent100: item.minimumPriceChangePercent100 !== undefined ? item.minimumPriceChangePercent100 : undefined,
841
- allocation: item.allocation ?
842
- typeof item.allocation === 'object' && Object.keys(item.allocation).length === 1 && Object.keys(item.allocation)[0] === 'id'
843
- ? { connect: {
844
- id: item.allocation.id
845
- }
846
- }
847
- : { connectOrCreate: {
848
- where: {
849
- id: item.allocation.id !== undefined ? item.allocation.id : undefined,
850
- alpacaAccountId: item.allocation.alpacaAccountId !== undefined ? item.allocation.alpacaAccountId : undefined,
851
- },
852
- create: {
853
- stocks: item.allocation.stocks !== undefined ? item.allocation.stocks : undefined,
854
- crypto: item.allocation.crypto !== undefined ? item.allocation.crypto : undefined,
855
- etfs: item.allocation.etfs !== undefined ? item.allocation.etfs : undefined,
856
- },
857
- }
1767
+ async updateMany(props, globalClient) {
1768
+ // Maximum number of retries for database connection issues
1769
+ const MAX_RETRIES = 3;
1770
+ let retryCount = 0;
1771
+ let lastError = null;
1772
+ // Retry loop to handle potential database connection issues
1773
+ while (retryCount < MAX_RETRIES) {
1774
+ try {
1775
+ const [modules, client] = await Promise.all([
1776
+ getApolloModules(),
1777
+ globalClient
1778
+ ? Promise.resolve(globalClient)
1779
+ : importedClient
1780
+ ]);
1781
+ const { gql, ApolloError } = modules;
1782
+ const UPDATE_MANY_USER = gql `
1783
+ mutation updateManyUser($data: [UserCreateManyInput!]!) {
1784
+ updateManyUser(data: $data) {
1785
+ count
1786
+ }
1787
+ }`;
1788
+ const variables = props.map(prop => ({
1789
+ where: {
1790
+ id: prop.id !== undefined ? prop.id : undefined,
1791
+ email: prop.email !== undefined ? prop.email : undefined,
1792
+ name: prop.name !== undefined ? prop.name : undefined,
1793
+ emailVerified: prop.emailVerified !== undefined ? {
1794
+ equals: prop.emailVerified
1795
+ } : undefined,
1796
+ image: prop.image !== undefined ? {
1797
+ equals: prop.image
1798
+ } : undefined,
1799
+ createdAt: prop.createdAt !== undefined ? {
1800
+ equals: prop.createdAt
1801
+ } : undefined,
1802
+ updatedAt: prop.updatedAt !== undefined ? {
1803
+ equals: prop.updatedAt
1804
+ } : undefined,
1805
+ role: prop.role !== undefined ? {
1806
+ equals: prop.role
1807
+ } : undefined,
1808
+ bio: prop.bio !== undefined ? {
1809
+ equals: prop.bio
1810
+ } : undefined,
1811
+ jobTitle: prop.jobTitle !== undefined ? {
1812
+ equals: prop.jobTitle
1813
+ } : undefined,
1814
+ currentAccount: prop.currentAccount !== undefined ? {
1815
+ equals: prop.currentAccount
1816
+ } : undefined,
1817
+ plan: prop.plan !== undefined ? {
1818
+ equals: prop.plan
1819
+ } : undefined,
1820
+ openaiAPIKey: prop.openaiAPIKey !== undefined ? {
1821
+ equals: prop.openaiAPIKey
1822
+ } : undefined,
1823
+ openaiModel: prop.openaiModel !== undefined ? {
1824
+ equals: prop.openaiModel
1825
+ } : undefined,
1826
+ },
1827
+ data: {
1828
+ id: prop.id !== undefined ? {
1829
+ set: prop.id
1830
+ } : undefined,
1831
+ name: prop.name !== undefined ? {
1832
+ set: prop.name
1833
+ } : undefined,
1834
+ email: prop.email !== undefined ? {
1835
+ set: prop.email
1836
+ } : undefined,
1837
+ emailVerified: prop.emailVerified !== undefined ? {
1838
+ set: prop.emailVerified
1839
+ } : undefined,
1840
+ image: prop.image !== undefined ? {
1841
+ set: prop.image
1842
+ } : undefined,
1843
+ createdAt: prop.createdAt !== undefined ? {
1844
+ set: prop.createdAt
1845
+ } : undefined,
1846
+ updatedAt: prop.updatedAt !== undefined ? {
1847
+ set: prop.updatedAt
1848
+ } : undefined,
1849
+ role: prop.role !== undefined ? {
1850
+ set: prop.role
1851
+ } : undefined,
1852
+ bio: prop.bio !== undefined ? {
1853
+ set: prop.bio
1854
+ } : undefined,
1855
+ jobTitle: prop.jobTitle !== undefined ? {
1856
+ set: prop.jobTitle
1857
+ } : undefined,
1858
+ currentAccount: prop.currentAccount !== undefined ? {
1859
+ set: prop.currentAccount
1860
+ } : undefined,
1861
+ plan: prop.plan !== undefined ? {
1862
+ set: prop.plan
1863
+ } : undefined,
1864
+ openaiAPIKey: prop.openaiAPIKey !== undefined ? {
1865
+ set: prop.openaiAPIKey
1866
+ } : undefined,
1867
+ openaiModel: prop.openaiModel !== undefined ? {
1868
+ set: prop.openaiModel
1869
+ } : undefined,
1870
+ customer: prop.customer ?
1871
+ typeof prop.customer === 'object' && Object.keys(prop.customer).length === 1 && (Object.keys(prop.customer)[0] === 'id' || Object.keys(prop.customer)[0] === 'symbol')
1872
+ ? {
1873
+ connect: {
1874
+ id: prop.customer.id
1875
+ }
1876
+ } : { upsert: {
1877
+ where: {
1878
+ id: prop.customer.id !== undefined ? {
1879
+ equals: prop.customer.id
1880
+ } : undefined,
1881
+ authUserId: prop.customer.authUserId !== undefined ? {
1882
+ equals: prop.customer.authUserId
1883
+ } : undefined,
1884
+ name: prop.customer.name !== undefined ? {
1885
+ equals: prop.customer.name
1886
+ } : undefined,
1887
+ stripeCustomerId: prop.customer.stripeCustomerId !== undefined ? {
1888
+ equals: prop.customer.stripeCustomerId
1889
+ } : undefined,
1890
+ stripeSubscriptionId: prop.customer.stripeSubscriptionId !== undefined ? {
1891
+ equals: prop.customer.stripeSubscriptionId
1892
+ } : undefined,
1893
+ stripePriceId: prop.customer.stripePriceId !== undefined ? {
1894
+ equals: prop.customer.stripePriceId
1895
+ } : undefined,
1896
+ },
1897
+ update: {
1898
+ authUserId: prop.customer.authUserId !== undefined ? {
1899
+ set: prop.customer.authUserId
1900
+ } : undefined,
1901
+ name: prop.customer.name !== undefined ? {
1902
+ set: prop.customer.name
1903
+ } : undefined,
1904
+ plan: prop.customer.plan !== undefined ? {
1905
+ set: prop.customer.plan
1906
+ } : undefined,
1907
+ stripeCustomerId: prop.customer.stripeCustomerId !== undefined ? {
1908
+ set: prop.customer.stripeCustomerId
1909
+ } : undefined,
1910
+ stripeSubscriptionId: prop.customer.stripeSubscriptionId !== undefined ? {
1911
+ set: prop.customer.stripeSubscriptionId
1912
+ } : undefined,
1913
+ stripePriceId: prop.customer.stripePriceId !== undefined ? {
1914
+ set: prop.customer.stripePriceId
1915
+ } : undefined,
1916
+ stripeCurrentPeriodEnd: prop.customer.stripeCurrentPeriodEnd !== undefined ? {
1917
+ set: prop.customer.stripeCurrentPeriodEnd
1918
+ } : undefined,
1919
+ },
1920
+ create: {
1921
+ authUserId: prop.customer.authUserId !== undefined ? prop.customer.authUserId : undefined,
1922
+ name: prop.customer.name !== undefined ? prop.customer.name : undefined,
1923
+ plan: prop.customer.plan !== undefined ? prop.customer.plan : undefined,
1924
+ stripeCustomerId: prop.customer.stripeCustomerId !== undefined ? prop.customer.stripeCustomerId : undefined,
1925
+ stripeSubscriptionId: prop.customer.stripeSubscriptionId !== undefined ? prop.customer.stripeSubscriptionId : undefined,
1926
+ stripePriceId: prop.customer.stripePriceId !== undefined ? prop.customer.stripePriceId : undefined,
1927
+ stripeCurrentPeriodEnd: prop.customer.stripeCurrentPeriodEnd !== undefined ? prop.customer.stripeCurrentPeriodEnd : undefined,
1928
+ },
1929
+ }
1930
+ } : undefined,
1931
+ accounts: prop.accounts ?
1932
+ Array.isArray(prop.accounts) && prop.accounts.length > 0 && prop.accounts.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
1933
+ connect: prop.accounts.map((item) => ({
1934
+ id: item.id
1935
+ }))
1936
+ } : { upsert: prop.accounts.map((item) => ({
1937
+ where: {
1938
+ id: item.id !== undefined ? item.id : undefined,
1939
+ providerAccountId: item.providerAccountId !== undefined ? item.providerAccountId : undefined,
1940
+ userId: item.userId !== undefined ? {
1941
+ equals: item.userId
1942
+ } : undefined,
1943
+ },
1944
+ update: {
1945
+ id: item.id !== undefined ? {
1946
+ set: item.id
1947
+ } : undefined,
1948
+ type: item.type !== undefined ? {
1949
+ set: item.type
1950
+ } : undefined,
1951
+ provider: item.provider !== undefined ? {
1952
+ set: item.provider
1953
+ } : undefined,
1954
+ providerAccountId: item.providerAccountId !== undefined ? {
1955
+ set: item.providerAccountId
1956
+ } : undefined,
1957
+ refresh_token: item.refresh_token !== undefined ? {
1958
+ set: item.refresh_token
1959
+ } : undefined,
1960
+ access_token: item.access_token !== undefined ? {
1961
+ set: item.access_token
1962
+ } : undefined,
1963
+ expires_at: item.expires_at !== undefined ? {
1964
+ set: item.expires_at
1965
+ } : undefined,
1966
+ token_type: item.token_type !== undefined ? {
1967
+ set: item.token_type
1968
+ } : undefined,
1969
+ scope: item.scope !== undefined ? {
1970
+ set: item.scope
1971
+ } : undefined,
1972
+ id_token: item.id_token !== undefined ? {
1973
+ set: item.id_token
1974
+ } : undefined,
1975
+ session_state: item.session_state !== undefined ? {
1976
+ set: item.session_state
1977
+ } : undefined,
1978
+ },
1979
+ create: {
1980
+ type: item.type !== undefined ? item.type : undefined,
1981
+ provider: item.provider !== undefined ? item.provider : undefined,
1982
+ providerAccountId: item.providerAccountId !== undefined ? item.providerAccountId : undefined,
1983
+ refresh_token: item.refresh_token !== undefined ? item.refresh_token : undefined,
1984
+ access_token: item.access_token !== undefined ? item.access_token : undefined,
1985
+ expires_at: item.expires_at !== undefined ? item.expires_at : undefined,
1986
+ token_type: item.token_type !== undefined ? item.token_type : undefined,
1987
+ scope: item.scope !== undefined ? item.scope : undefined,
1988
+ id_token: item.id_token !== undefined ? item.id_token : undefined,
1989
+ session_state: item.session_state !== undefined ? item.session_state : undefined,
1990
+ },
1991
+ }))
1992
+ } : undefined,
1993
+ sessions: prop.sessions ?
1994
+ Array.isArray(prop.sessions) && prop.sessions.length > 0 && prop.sessions.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
1995
+ connect: prop.sessions.map((item) => ({
1996
+ id: item.id
1997
+ }))
1998
+ } : { upsert: prop.sessions.map((item) => ({
1999
+ where: {
2000
+ id: item.id !== undefined ? item.id : undefined,
2001
+ userId: item.userId !== undefined ? {
2002
+ equals: item.userId
2003
+ } : undefined,
2004
+ },
2005
+ update: {
2006
+ id: item.id !== undefined ? {
2007
+ set: item.id
2008
+ } : undefined,
2009
+ sessionToken: item.sessionToken !== undefined ? {
2010
+ set: item.sessionToken
2011
+ } : undefined,
2012
+ expires: item.expires !== undefined ? {
2013
+ set: item.expires
2014
+ } : undefined,
2015
+ },
2016
+ create: {
2017
+ sessionToken: item.sessionToken !== undefined ? item.sessionToken : undefined,
2018
+ expires: item.expires !== undefined ? item.expires : undefined,
2019
+ },
2020
+ }))
2021
+ } : undefined,
2022
+ authenticators: prop.authenticators ?
2023
+ Array.isArray(prop.authenticators) && prop.authenticators.length > 0 && prop.authenticators.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
2024
+ connect: prop.authenticators.map((item) => ({
2025
+ id: item.id
2026
+ }))
2027
+ } : { upsert: prop.authenticators.map((item) => ({
2028
+ where: {
2029
+ id: item.id !== undefined ? item.id : undefined,
2030
+ userId: item.userId !== undefined ? {
2031
+ equals: item.userId
2032
+ } : undefined,
2033
+ },
2034
+ update: {
2035
+ id: item.id !== undefined ? {
2036
+ set: item.id
2037
+ } : undefined,
2038
+ credentialID: item.credentialID !== undefined ? {
2039
+ set: item.credentialID
2040
+ } : undefined,
2041
+ publicKey: item.publicKey !== undefined ? {
2042
+ set: item.publicKey
2043
+ } : undefined,
2044
+ counter: item.counter !== undefined ? {
2045
+ set: item.counter
2046
+ } : undefined,
2047
+ },
2048
+ create: {
2049
+ credentialID: item.credentialID !== undefined ? item.credentialID : undefined,
2050
+ publicKey: item.publicKey !== undefined ? item.publicKey : undefined,
2051
+ counter: item.counter !== undefined ? item.counter : undefined,
2052
+ },
2053
+ }))
2054
+ } : undefined,
2055
+ alpacaAccounts: prop.alpacaAccounts ?
2056
+ Array.isArray(prop.alpacaAccounts) && prop.alpacaAccounts.length > 0 && prop.alpacaAccounts.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
2057
+ connect: prop.alpacaAccounts.map((item) => ({
2058
+ id: item.id
2059
+ }))
2060
+ } : { upsert: prop.alpacaAccounts.map((item) => ({
2061
+ where: {
2062
+ id: item.id !== undefined ? item.id : undefined,
2063
+ userId: item.userId !== undefined ? {
2064
+ equals: item.userId
2065
+ } : undefined,
2066
+ },
2067
+ update: {
2068
+ id: item.id !== undefined ? {
2069
+ set: item.id
2070
+ } : undefined,
2071
+ type: item.type !== undefined ? {
2072
+ set: item.type
2073
+ } : undefined,
2074
+ APIKey: item.APIKey !== undefined ? {
2075
+ set: item.APIKey
2076
+ } : undefined,
2077
+ APISecret: item.APISecret !== undefined ? {
2078
+ set: item.APISecret
2079
+ } : undefined,
2080
+ configuration: item.configuration !== undefined ? {
2081
+ set: item.configuration
2082
+ } : undefined,
2083
+ marketOpen: item.marketOpen !== undefined ? {
2084
+ set: item.marketOpen
2085
+ } : undefined,
2086
+ realTime: item.realTime !== undefined ? {
2087
+ set: item.realTime
2088
+ } : undefined,
2089
+ cryptoTradingEnabled: item.cryptoTradingEnabled !== undefined ? {
2090
+ set: item.cryptoTradingEnabled
2091
+ } : undefined,
2092
+ cryptoTradingPairs: item.cryptoTradingPairs !== undefined ? {
2093
+ set: item.cryptoTradingPairs
2094
+ } : undefined,
2095
+ cryptoTradeAllocationPct: item.cryptoTradeAllocationPct !== undefined ? {
2096
+ set: item.cryptoTradeAllocationPct
2097
+ } : undefined,
2098
+ tradeAllocationPct: item.tradeAllocationPct !== undefined ? {
2099
+ set: item.tradeAllocationPct
2100
+ } : undefined,
2101
+ minPercentageChange: item.minPercentageChange !== undefined ? {
2102
+ set: item.minPercentageChange
858
2103
  } : undefined,
859
- alerts: item.alerts ?
860
- Array.isArray(item.alerts) && item.alerts.length > 0 && item.alerts.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
861
- connect: item.alerts.map((item) => ({
862
- id: item.id
863
- }))
864
- }
865
- : { connectOrCreate: item.alerts.map((item) => ({
866
- where: {
867
- id: item.id !== undefined ? item.id : undefined,
868
- alpacaAccountId: item.alpacaAccountId !== undefined ? {
869
- equals: item.alpacaAccountId
870
- } : undefined,
871
- },
872
- create: {
873
- message: item.message !== undefined ? item.message : undefined,
874
- type: item.type !== undefined ? item.type : undefined,
875
- isRead: item.isRead !== undefined ? item.isRead : undefined,
876
- },
877
- }))
878
- } : undefined,
879
- },
880
- }))
881
- } : undefined,
882
- },
883
- };
884
- const filteredVariables = removeUndefinedProps(variables);
885
- try {
886
- const response = await client.mutate({ mutation: UPDATE_ONE_USER, variables: filteredVariables });
887
- if (response.errors && response.errors.length > 0)
888
- throw new Error(response.errors[0].message);
889
- if (response && response.data && response.data.updateOneUser) {
890
- return response.data.updateOneUser;
891
- }
892
- else {
893
- return null;
894
- }
895
- }
896
- catch (error) {
897
- console.error('Error in updateOneUser:', error);
898
- throw error;
899
- }
900
- },
901
- /**
902
- * Upsert a single User record.
903
- * @param props - Properties to update.
904
- * @param globalClient - Apollo Client instance.
905
- * @returns The updated User or null.
906
- */
907
- async upsert(props, globalClient) {
908
- const [modules, client] = await Promise.all([
909
- getApolloModules(),
910
- globalClient
911
- ? Promise.resolve(globalClient)
912
- : importedClient
913
- ]);
914
- const { gql, ApolloError } = modules;
915
- const UPSERT_ONE_USER = gql `
916
- mutation upsertOneUser($where: UserWhereUniqueInput!, $create: UserCreateInput!, $update: UserUpdateInput!) {
917
- upsertOneUser(where: $where, create: $create, update: $update) {
918
- ${selectionSet}
919
- }
920
- }`;
921
- const variables = {
922
- where: {
923
- id: props.id !== undefined ? props.id : undefined,
924
- email: props.email !== undefined ? props.email : undefined,
925
- name: props.name !== undefined ? {
926
- equals: props.name
927
- } : undefined,
928
- },
929
- create: {
930
- name: props.name !== undefined ? props.name : undefined,
931
- email: props.email !== undefined ? props.email : undefined,
932
- emailVerified: props.emailVerified !== undefined ? props.emailVerified : undefined,
933
- image: props.image !== undefined ? props.image : undefined,
934
- role: props.role !== undefined ? props.role : undefined,
935
- bio: props.bio !== undefined ? props.bio : undefined,
936
- jobTitle: props.jobTitle !== undefined ? props.jobTitle : undefined,
937
- currentAccount: props.currentAccount !== undefined ? props.currentAccount : undefined,
938
- plan: props.plan !== undefined ? props.plan : undefined,
939
- openaiAPIKey: props.openaiAPIKey !== undefined ? props.openaiAPIKey : undefined,
940
- openaiModel: props.openaiModel !== undefined ? props.openaiModel : undefined,
941
- customer: props.customer ?
942
- typeof props.customer === 'object' && Object.keys(props.customer).length === 1 && Object.keys(props.customer)[0] === 'id'
943
- ? { connect: {
944
- id: props.customer.id
945
- }
946
- }
947
- : { connectOrCreate: {
948
- where: {
949
- id: props.customer.id !== undefined ? props.customer.id : undefined,
950
- stripeCustomerId: props.customer.stripeCustomerId !== undefined ? props.customer.stripeCustomerId : undefined,
951
- stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? props.customer.stripeSubscriptionId : undefined,
952
- authUserId: props.customer.authUserId !== undefined ? {
953
- equals: props.customer.authUserId
954
- } : undefined,
955
- name: props.customer.name !== undefined ? {
956
- equals: props.customer.name
957
- } : undefined,
958
- stripePriceId: props.customer.stripePriceId !== undefined ? {
959
- equals: props.customer.stripePriceId
960
- } : undefined,
961
- },
962
- create: {
963
- authUserId: props.customer.authUserId !== undefined ? props.customer.authUserId : undefined,
964
- name: props.customer.name !== undefined ? props.customer.name : undefined,
965
- plan: props.customer.plan !== undefined ? props.customer.plan : undefined,
966
- stripeCustomerId: props.customer.stripeCustomerId !== undefined ? props.customer.stripeCustomerId : undefined,
967
- stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? props.customer.stripeSubscriptionId : undefined,
968
- stripePriceId: props.customer.stripePriceId !== undefined ? props.customer.stripePriceId : undefined,
969
- stripeCurrentPeriodEnd: props.customer.stripeCurrentPeriodEnd !== undefined ? props.customer.stripeCurrentPeriodEnd : undefined,
970
- },
971
- }
972
- } : undefined,
973
- accounts: props.accounts ?
974
- Array.isArray(props.accounts) && props.accounts.length > 0 && props.accounts.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
975
- connect: props.accounts.map((item) => ({
976
- id: item.id
977
- }))
978
- }
979
- : { connectOrCreate: props.accounts.map((item) => ({
980
- where: {
981
- id: item.id !== undefined ? item.id : undefined,
982
- userId: item.userId !== undefined ? {
983
- equals: item.userId
984
- } : undefined,
985
- providerAccountId: item.providerAccountId !== undefined ? {
986
- equals: item.providerAccountId
987
- } : undefined,
988
- },
989
- create: {
990
- type: item.type !== undefined ? item.type : undefined,
991
- provider: item.provider !== undefined ? item.provider : undefined,
992
- providerAccountId: item.providerAccountId !== undefined ? item.providerAccountId : undefined,
993
- refresh_token: item.refresh_token !== undefined ? item.refresh_token : undefined,
994
- access_token: item.access_token !== undefined ? item.access_token : undefined,
995
- expires_at: item.expires_at !== undefined ? item.expires_at : undefined,
996
- token_type: item.token_type !== undefined ? item.token_type : undefined,
997
- scope: item.scope !== undefined ? item.scope : undefined,
998
- id_token: item.id_token !== undefined ? item.id_token : undefined,
999
- session_state: item.session_state !== undefined ? item.session_state : undefined,
1000
- },
1001
- }))
1002
- } : undefined,
1003
- sessions: props.sessions ?
1004
- Array.isArray(props.sessions) && props.sessions.length > 0 && props.sessions.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
1005
- connect: props.sessions.map((item) => ({
1006
- id: item.id
1007
- }))
1008
- }
1009
- : { connectOrCreate: props.sessions.map((item) => ({
1010
- where: {
1011
- id: item.id !== undefined ? item.id : undefined,
1012
- userId: item.userId !== undefined ? {
1013
- equals: item.userId
1014
- } : undefined,
1015
- },
1016
- create: {
1017
- sessionToken: item.sessionToken !== undefined ? item.sessionToken : undefined,
1018
- expires: item.expires !== undefined ? item.expires : undefined,
1019
- },
1020
- }))
1021
- } : undefined,
1022
- authenticators: props.authenticators ?
1023
- Array.isArray(props.authenticators) && props.authenticators.length > 0 && props.authenticators.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
1024
- connect: props.authenticators.map((item) => ({
1025
- id: item.id
1026
- }))
1027
- }
1028
- : { connectOrCreate: props.authenticators.map((item) => ({
1029
- where: {
1030
- id: item.id !== undefined ? item.id : undefined,
1031
- userId: item.userId !== undefined ? {
1032
- equals: item.userId
1033
- } : undefined,
1034
- },
1035
- create: {
1036
- credentialID: item.credentialID !== undefined ? item.credentialID : undefined,
1037
- publicKey: item.publicKey !== undefined ? item.publicKey : undefined,
1038
- counter: item.counter !== undefined ? item.counter : undefined,
1039
- },
1040
- }))
1041
- } : undefined,
1042
- alpacaAccounts: props.alpacaAccounts ?
1043
- Array.isArray(props.alpacaAccounts) && props.alpacaAccounts.length > 0 && props.alpacaAccounts.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
1044
- connect: props.alpacaAccounts.map((item) => ({
1045
- id: item.id
1046
- }))
1047
- }
1048
- : { connectOrCreate: props.alpacaAccounts.map((item) => ({
1049
- where: {
1050
- id: item.id !== undefined ? item.id : undefined,
1051
- userId: item.userId !== undefined ? {
1052
- equals: item.userId
1053
- } : undefined,
1054
- },
1055
- create: {
1056
- type: item.type !== undefined ? item.type : undefined,
1057
- APIKey: item.APIKey !== undefined ? item.APIKey : undefined,
1058
- APISecret: item.APISecret !== undefined ? item.APISecret : undefined,
1059
- configuration: item.configuration !== undefined ? item.configuration : undefined,
1060
- marketOpen: item.marketOpen !== undefined ? item.marketOpen : undefined,
1061
- realTime: item.realTime !== undefined ? item.realTime : undefined,
1062
- cryptoTradingEnabled: item.cryptoTradingEnabled !== undefined ? item.cryptoTradingEnabled : undefined,
1063
- cryptoTradingPairs: item.cryptoTradingPairs !== undefined ? {
1064
- set: item.cryptoTradingPairs
1065
- } : undefined,
1066
- cryptoTradeAllocationPct: item.cryptoTradeAllocationPct !== undefined ? item.cryptoTradeAllocationPct : undefined,
1067
- tradeAllocationPct: item.tradeAllocationPct !== undefined ? item.tradeAllocationPct : undefined,
1068
- minPercentageChange: item.minPercentageChange !== undefined ? item.minPercentageChange : undefined,
1069
- volumeThreshold: item.volumeThreshold !== undefined ? item.volumeThreshold : undefined,
1070
- enablePortfolioTrailingStop: item.enablePortfolioTrailingStop !== undefined ? item.enablePortfolioTrailingStop : undefined,
1071
- portfolioTrailPercent: item.portfolioTrailPercent !== undefined ? item.portfolioTrailPercent : undefined,
1072
- portfolioProfitThresholdPercent: item.portfolioProfitThresholdPercent !== undefined ? item.portfolioProfitThresholdPercent : undefined,
1073
- reducedPortfolioTrailPercent: item.reducedPortfolioTrailPercent !== undefined ? item.reducedPortfolioTrailPercent : undefined,
1074
- defaultTrailingStopPercentage100: item.defaultTrailingStopPercentage100 !== undefined ? item.defaultTrailingStopPercentage100 : undefined,
1075
- firstTrailReductionThreshold100: item.firstTrailReductionThreshold100 !== undefined ? item.firstTrailReductionThreshold100 : undefined,
1076
- secondTrailReductionThreshold100: item.secondTrailReductionThreshold100 !== undefined ? item.secondTrailReductionThreshold100 : undefined,
1077
- firstReducedTrailPercentage100: item.firstReducedTrailPercentage100 !== undefined ? item.firstReducedTrailPercentage100 : undefined,
1078
- secondReducedTrailPercentage100: item.secondReducedTrailPercentage100 !== undefined ? item.secondReducedTrailPercentage100 : undefined,
1079
- minimumPriceChangePercent100: item.minimumPriceChangePercent100 !== undefined ? item.minimumPriceChangePercent100 : undefined,
1080
- allocation: item.allocation ?
1081
- typeof item.allocation === 'object' && Object.keys(item.allocation).length === 1 && Object.keys(item.allocation)[0] === 'id'
1082
- ? { connect: {
1083
- id: item.allocation.id
1084
- }
1085
- }
1086
- : { connectOrCreate: {
2104
+ volumeThreshold: item.volumeThreshold !== undefined ? {
2105
+ set: item.volumeThreshold
2106
+ } : undefined,
2107
+ enablePortfolioTrailingStop: item.enablePortfolioTrailingStop !== undefined ? {
2108
+ set: item.enablePortfolioTrailingStop
2109
+ } : undefined,
2110
+ portfolioTrailPercent: item.portfolioTrailPercent !== undefined ? {
2111
+ set: item.portfolioTrailPercent
2112
+ } : undefined,
2113
+ portfolioProfitThresholdPercent: item.portfolioProfitThresholdPercent !== undefined ? {
2114
+ set: item.portfolioProfitThresholdPercent
2115
+ } : undefined,
2116
+ reducedPortfolioTrailPercent: item.reducedPortfolioTrailPercent !== undefined ? {
2117
+ set: item.reducedPortfolioTrailPercent
2118
+ } : undefined,
2119
+ defaultTrailingStopPercentage100: item.defaultTrailingStopPercentage100 !== undefined ? {
2120
+ set: item.defaultTrailingStopPercentage100
2121
+ } : undefined,
2122
+ firstTrailReductionThreshold100: item.firstTrailReductionThreshold100 !== undefined ? {
2123
+ set: item.firstTrailReductionThreshold100
2124
+ } : undefined,
2125
+ secondTrailReductionThreshold100: item.secondTrailReductionThreshold100 !== undefined ? {
2126
+ set: item.secondTrailReductionThreshold100
2127
+ } : undefined,
2128
+ firstReducedTrailPercentage100: item.firstReducedTrailPercentage100 !== undefined ? {
2129
+ set: item.firstReducedTrailPercentage100
2130
+ } : undefined,
2131
+ secondReducedTrailPercentage100: item.secondReducedTrailPercentage100 !== undefined ? {
2132
+ set: item.secondReducedTrailPercentage100
2133
+ } : undefined,
2134
+ minimumPriceChangePercent100: item.minimumPriceChangePercent100 !== undefined ? {
2135
+ set: item.minimumPriceChangePercent100
2136
+ } : undefined,
2137
+ allocation: item.allocation ?
2138
+ typeof item.allocation === 'object' && Object.keys(item.allocation).length === 1 && (Object.keys(item.allocation)[0] === 'id' || Object.keys(item.allocation)[0] === 'symbol')
2139
+ ? {
2140
+ connect: {
2141
+ id: item.allocation.id
2142
+ }
2143
+ } : { upsert: {
1087
2144
  where: {
1088
- id: item.allocation.id !== undefined ? item.allocation.id : undefined,
1089
- alpacaAccountId: item.allocation.alpacaAccountId !== undefined ? item.allocation.alpacaAccountId : undefined,
2145
+ id: item.allocation.id !== undefined ? {
2146
+ equals: item.allocation.id
2147
+ } : undefined,
2148
+ alpacaAccountId: item.allocation.alpacaAccountId !== undefined ? {
2149
+ equals: item.allocation.alpacaAccountId
2150
+ } : undefined,
2151
+ },
2152
+ update: {
2153
+ id: item.allocation.id !== undefined ? {
2154
+ set: item.allocation.id
2155
+ } : undefined,
2156
+ stocks: item.allocation.stocks !== undefined ? {
2157
+ set: item.allocation.stocks
2158
+ } : undefined,
2159
+ crypto: item.allocation.crypto !== undefined ? {
2160
+ set: item.allocation.crypto
2161
+ } : undefined,
2162
+ etfs: item.allocation.etfs !== undefined ? {
2163
+ set: item.allocation.etfs
2164
+ } : undefined,
1090
2165
  },
1091
2166
  create: {
1092
2167
  stocks: item.allocation.stocks !== undefined ? item.allocation.stocks : undefined,
@@ -1095,19 +2170,32 @@ export const User = {
1095
2170
  },
1096
2171
  }
1097
2172
  } : undefined,
1098
- alerts: item.alerts ?
1099
- Array.isArray(item.alerts) && item.alerts.length > 0 && item.alerts.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
1100
- connect: item.alerts.map((item) => ({
1101
- id: item.id
1102
- }))
1103
- }
1104
- : { connectOrCreate: item.alerts.map((item) => ({
2173
+ alerts: item.alerts ?
2174
+ Array.isArray(item.alerts) && item.alerts.length > 0 && item.alerts.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
2175
+ connect: item.alerts.map((item) => ({
2176
+ id: item.id
2177
+ }))
2178
+ } : { upsert: item.alerts.map((item) => ({
1105
2179
  where: {
1106
2180
  id: item.id !== undefined ? item.id : undefined,
1107
2181
  alpacaAccountId: item.alpacaAccountId !== undefined ? {
1108
2182
  equals: item.alpacaAccountId
1109
2183
  } : undefined,
1110
2184
  },
2185
+ update: {
2186
+ id: item.id !== undefined ? {
2187
+ set: item.id
2188
+ } : undefined,
2189
+ message: item.message !== undefined ? {
2190
+ set: item.message
2191
+ } : undefined,
2192
+ type: item.type !== undefined ? {
2193
+ set: item.type
2194
+ } : undefined,
2195
+ isRead: item.isRead !== undefined ? {
2196
+ set: item.isRead
2197
+ } : undefined,
2198
+ },
1111
2199
  create: {
1112
2200
  message: item.message !== undefined ? item.message : undefined,
1113
2201
  type: item.type !== undefined ? item.type : undefined,
@@ -1115,1140 +2203,464 @@ export const User = {
1115
2203
  },
1116
2204
  }))
1117
2205
  } : undefined,
1118
- },
1119
- }))
1120
- } : undefined,
1121
- },
1122
- update: {
1123
- name: props.name !== undefined ? {
1124
- set: props.name
1125
- } : undefined,
1126
- email: props.email !== undefined ? {
1127
- set: props.email
1128
- } : undefined,
1129
- emailVerified: props.emailVerified !== undefined ? {
1130
- set: props.emailVerified
1131
- } : undefined,
1132
- image: props.image !== undefined ? {
1133
- set: props.image
1134
- } : undefined,
1135
- role: props.role !== undefined ? {
1136
- set: props.role
1137
- } : undefined,
1138
- bio: props.bio !== undefined ? {
1139
- set: props.bio
1140
- } : undefined,
1141
- jobTitle: props.jobTitle !== undefined ? {
1142
- set: props.jobTitle
1143
- } : undefined,
1144
- currentAccount: props.currentAccount !== undefined ? {
1145
- set: props.currentAccount
1146
- } : undefined,
1147
- plan: props.plan !== undefined ? {
1148
- set: props.plan
1149
- } : undefined,
1150
- openaiAPIKey: props.openaiAPIKey !== undefined ? {
1151
- set: props.openaiAPIKey
1152
- } : undefined,
1153
- openaiModel: props.openaiModel !== undefined ? {
1154
- set: props.openaiModel
1155
- } : undefined,
1156
- customer: props.customer ?
1157
- typeof props.customer === 'object' && Object.keys(props.customer).length === 1 && (Object.keys(props.customer)[0] === 'id' || Object.keys(props.customer)[0] === 'symbol')
1158
- ? {
1159
- connect: {
1160
- id: props.customer.id
1161
- }
1162
- } : { upsert: {
1163
- where: {
1164
- id: props.customer.id !== undefined ? {
1165
- equals: props.customer.id
1166
- } : undefined,
1167
- authUserId: props.customer.authUserId !== undefined ? {
1168
- equals: props.customer.authUserId
1169
- } : undefined,
1170
- name: props.customer.name !== undefined ? {
1171
- equals: props.customer.name
1172
- } : undefined,
1173
- stripeCustomerId: props.customer.stripeCustomerId !== undefined ? {
1174
- equals: props.customer.stripeCustomerId
1175
- } : undefined,
1176
- stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? {
1177
- equals: props.customer.stripeSubscriptionId
1178
- } : undefined,
1179
- stripePriceId: props.customer.stripePriceId !== undefined ? {
1180
- equals: props.customer.stripePriceId
1181
- } : undefined,
1182
- },
1183
- update: {
1184
- authUserId: props.customer.authUserId !== undefined ? {
1185
- set: props.customer.authUserId
1186
- } : undefined,
1187
- name: props.customer.name !== undefined ? {
1188
- set: props.customer.name
1189
- } : undefined,
1190
- plan: props.customer.plan !== undefined ? {
1191
- set: props.customer.plan
1192
- } : undefined,
1193
- stripeCustomerId: props.customer.stripeCustomerId !== undefined ? {
1194
- set: props.customer.stripeCustomerId
1195
- } : undefined,
1196
- stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? {
1197
- set: props.customer.stripeSubscriptionId
1198
- } : undefined,
1199
- stripePriceId: props.customer.stripePriceId !== undefined ? {
1200
- set: props.customer.stripePriceId
1201
- } : undefined,
1202
- stripeCurrentPeriodEnd: props.customer.stripeCurrentPeriodEnd !== undefined ? {
1203
- set: props.customer.stripeCurrentPeriodEnd
1204
- } : undefined,
1205
- },
1206
- create: {
1207
- authUserId: props.customer.authUserId !== undefined ? props.customer.authUserId : undefined,
1208
- name: props.customer.name !== undefined ? props.customer.name : undefined,
1209
- plan: props.customer.plan !== undefined ? props.customer.plan : undefined,
1210
- stripeCustomerId: props.customer.stripeCustomerId !== undefined ? props.customer.stripeCustomerId : undefined,
1211
- stripeSubscriptionId: props.customer.stripeSubscriptionId !== undefined ? props.customer.stripeSubscriptionId : undefined,
1212
- stripePriceId: props.customer.stripePriceId !== undefined ? props.customer.stripePriceId : undefined,
1213
- stripeCurrentPeriodEnd: props.customer.stripeCurrentPeriodEnd !== undefined ? props.customer.stripeCurrentPeriodEnd : undefined,
1214
- },
1215
- }
1216
- } : undefined,
1217
- accounts: props.accounts ?
1218
- Array.isArray(props.accounts) && props.accounts.length > 0 && props.accounts.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
1219
- connect: props.accounts.map((item) => ({
1220
- id: item.id
1221
- }))
1222
- } : { upsert: props.accounts.map((item) => ({
1223
- where: {
1224
- id: item.id !== undefined ? item.id : undefined,
1225
- userId: item.userId !== undefined ? {
1226
- equals: item.userId
1227
- } : undefined,
1228
- providerAccountId: item.providerAccountId !== undefined ? {
1229
- equals: item.providerAccountId
1230
- } : undefined,
1231
- },
1232
- update: {
1233
- id: item.id !== undefined ? {
1234
- set: item.id
1235
- } : undefined,
1236
- type: item.type !== undefined ? {
1237
- set: item.type
1238
- } : undefined,
1239
- provider: item.provider !== undefined ? {
1240
- set: item.provider
1241
- } : undefined,
1242
- providerAccountId: item.providerAccountId !== undefined ? {
1243
- set: item.providerAccountId
1244
- } : undefined,
1245
- refresh_token: item.refresh_token !== undefined ? {
1246
- set: item.refresh_token
1247
- } : undefined,
1248
- access_token: item.access_token !== undefined ? {
1249
- set: item.access_token
1250
- } : undefined,
1251
- expires_at: item.expires_at !== undefined ? {
1252
- set: item.expires_at
1253
- } : undefined,
1254
- token_type: item.token_type !== undefined ? {
1255
- set: item.token_type
1256
- } : undefined,
1257
- scope: item.scope !== undefined ? {
1258
- set: item.scope
1259
- } : undefined,
1260
- id_token: item.id_token !== undefined ? {
1261
- set: item.id_token
1262
- } : undefined,
1263
- session_state: item.session_state !== undefined ? {
1264
- set: item.session_state
1265
- } : undefined,
1266
- },
1267
- create: {
1268
- type: item.type !== undefined ? item.type : undefined,
1269
- provider: item.provider !== undefined ? item.provider : undefined,
1270
- providerAccountId: item.providerAccountId !== undefined ? item.providerAccountId : undefined,
1271
- refresh_token: item.refresh_token !== undefined ? item.refresh_token : undefined,
1272
- access_token: item.access_token !== undefined ? item.access_token : undefined,
1273
- expires_at: item.expires_at !== undefined ? item.expires_at : undefined,
1274
- token_type: item.token_type !== undefined ? item.token_type : undefined,
1275
- scope: item.scope !== undefined ? item.scope : undefined,
1276
- id_token: item.id_token !== undefined ? item.id_token : undefined,
1277
- session_state: item.session_state !== undefined ? item.session_state : undefined,
1278
- },
1279
- }))
1280
- } : undefined,
1281
- sessions: props.sessions ?
1282
- Array.isArray(props.sessions) && props.sessions.length > 0 && props.sessions.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
1283
- connect: props.sessions.map((item) => ({
1284
- id: item.id
1285
- }))
1286
- } : { upsert: props.sessions.map((item) => ({
1287
- where: {
1288
- id: item.id !== undefined ? item.id : undefined,
1289
- userId: item.userId !== undefined ? {
1290
- equals: item.userId
1291
- } : undefined,
1292
- },
1293
- update: {
1294
- id: item.id !== undefined ? {
1295
- set: item.id
1296
- } : undefined,
1297
- sessionToken: item.sessionToken !== undefined ? {
1298
- set: item.sessionToken
1299
- } : undefined,
1300
- expires: item.expires !== undefined ? {
1301
- set: item.expires
1302
- } : undefined,
1303
- },
1304
- create: {
1305
- sessionToken: item.sessionToken !== undefined ? item.sessionToken : undefined,
1306
- expires: item.expires !== undefined ? item.expires : undefined,
1307
- },
1308
- }))
1309
- } : undefined,
1310
- authenticators: props.authenticators ?
1311
- Array.isArray(props.authenticators) && props.authenticators.length > 0 && props.authenticators.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
1312
- connect: props.authenticators.map((item) => ({
1313
- id: item.id
1314
- }))
1315
- } : { upsert: props.authenticators.map((item) => ({
1316
- where: {
1317
- id: item.id !== undefined ? item.id : undefined,
1318
- userId: item.userId !== undefined ? {
1319
- equals: item.userId
1320
- } : undefined,
1321
- },
1322
- update: {
1323
- id: item.id !== undefined ? {
1324
- set: item.id
1325
- } : undefined,
1326
- credentialID: item.credentialID !== undefined ? {
1327
- set: item.credentialID
1328
- } : undefined,
1329
- publicKey: item.publicKey !== undefined ? {
1330
- set: item.publicKey
1331
- } : undefined,
1332
- counter: item.counter !== undefined ? {
1333
- set: item.counter
1334
- } : undefined,
1335
- },
1336
- create: {
1337
- credentialID: item.credentialID !== undefined ? item.credentialID : undefined,
1338
- publicKey: item.publicKey !== undefined ? item.publicKey : undefined,
1339
- counter: item.counter !== undefined ? item.counter : undefined,
1340
- },
1341
- }))
1342
- } : undefined,
1343
- alpacaAccounts: props.alpacaAccounts ?
1344
- Array.isArray(props.alpacaAccounts) && props.alpacaAccounts.length > 0 && props.alpacaAccounts.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
1345
- connect: props.alpacaAccounts.map((item) => ({
1346
- id: item.id
1347
- }))
1348
- } : { upsert: props.alpacaAccounts.map((item) => ({
1349
- where: {
1350
- id: item.id !== undefined ? item.id : undefined,
1351
- userId: item.userId !== undefined ? {
1352
- equals: item.userId
1353
- } : undefined,
1354
- },
1355
- update: {
1356
- id: item.id !== undefined ? {
1357
- set: item.id
1358
- } : undefined,
1359
- type: item.type !== undefined ? {
1360
- set: item.type
1361
- } : undefined,
1362
- APIKey: item.APIKey !== undefined ? {
1363
- set: item.APIKey
1364
- } : undefined,
1365
- APISecret: item.APISecret !== undefined ? {
1366
- set: item.APISecret
1367
- } : undefined,
1368
- configuration: item.configuration !== undefined ? {
1369
- set: item.configuration
1370
- } : undefined,
1371
- marketOpen: item.marketOpen !== undefined ? {
1372
- set: item.marketOpen
1373
- } : undefined,
1374
- realTime: item.realTime !== undefined ? {
1375
- set: item.realTime
1376
- } : undefined,
1377
- cryptoTradingEnabled: item.cryptoTradingEnabled !== undefined ? {
1378
- set: item.cryptoTradingEnabled
1379
- } : undefined,
1380
- cryptoTradingPairs: item.cryptoTradingPairs !== undefined ? {
1381
- set: item.cryptoTradingPairs
1382
- } : undefined,
1383
- cryptoTradeAllocationPct: item.cryptoTradeAllocationPct !== undefined ? {
1384
- set: item.cryptoTradeAllocationPct
1385
- } : undefined,
1386
- tradeAllocationPct: item.tradeAllocationPct !== undefined ? {
1387
- set: item.tradeAllocationPct
1388
- } : undefined,
1389
- minPercentageChange: item.minPercentageChange !== undefined ? {
1390
- set: item.minPercentageChange
1391
- } : undefined,
1392
- volumeThreshold: item.volumeThreshold !== undefined ? {
1393
- set: item.volumeThreshold
1394
- } : undefined,
1395
- enablePortfolioTrailingStop: item.enablePortfolioTrailingStop !== undefined ? {
1396
- set: item.enablePortfolioTrailingStop
1397
- } : undefined,
1398
- portfolioTrailPercent: item.portfolioTrailPercent !== undefined ? {
1399
- set: item.portfolioTrailPercent
1400
- } : undefined,
1401
- portfolioProfitThresholdPercent: item.portfolioProfitThresholdPercent !== undefined ? {
1402
- set: item.portfolioProfitThresholdPercent
1403
- } : undefined,
1404
- reducedPortfolioTrailPercent: item.reducedPortfolioTrailPercent !== undefined ? {
1405
- set: item.reducedPortfolioTrailPercent
1406
- } : undefined,
1407
- defaultTrailingStopPercentage100: item.defaultTrailingStopPercentage100 !== undefined ? {
1408
- set: item.defaultTrailingStopPercentage100
1409
- } : undefined,
1410
- firstTrailReductionThreshold100: item.firstTrailReductionThreshold100 !== undefined ? {
1411
- set: item.firstTrailReductionThreshold100
1412
- } : undefined,
1413
- secondTrailReductionThreshold100: item.secondTrailReductionThreshold100 !== undefined ? {
1414
- set: item.secondTrailReductionThreshold100
1415
- } : undefined,
1416
- firstReducedTrailPercentage100: item.firstReducedTrailPercentage100 !== undefined ? {
1417
- set: item.firstReducedTrailPercentage100
1418
- } : undefined,
1419
- secondReducedTrailPercentage100: item.secondReducedTrailPercentage100 !== undefined ? {
1420
- set: item.secondReducedTrailPercentage100
1421
- } : undefined,
1422
- minimumPriceChangePercent100: item.minimumPriceChangePercent100 !== undefined ? {
1423
- set: item.minimumPriceChangePercent100
1424
- } : undefined,
1425
- allocation: item.allocation ?
1426
- typeof item.allocation === 'object' && Object.keys(item.allocation).length === 1 && (Object.keys(item.allocation)[0] === 'id' || Object.keys(item.allocation)[0] === 'symbol')
1427
- ? {
1428
- connect: {
1429
- id: item.allocation.id
1430
- }
1431
- } : { upsert: {
1432
- where: {
1433
- id: item.allocation.id !== undefined ? {
1434
- equals: item.allocation.id
1435
- } : undefined,
1436
- alpacaAccountId: item.allocation.alpacaAccountId !== undefined ? {
1437
- equals: item.allocation.alpacaAccountId
1438
- } : undefined,
1439
- },
1440
- update: {
1441
- id: item.allocation.id !== undefined ? {
1442
- set: item.allocation.id
1443
- } : undefined,
1444
- stocks: item.allocation.stocks !== undefined ? {
1445
- set: item.allocation.stocks
1446
- } : undefined,
1447
- crypto: item.allocation.crypto !== undefined ? {
1448
- set: item.allocation.crypto
1449
- } : undefined,
1450
- etfs: item.allocation.etfs !== undefined ? {
1451
- set: item.allocation.etfs
1452
- } : undefined,
1453
- },
1454
- create: {
1455
- stocks: item.allocation.stocks !== undefined ? item.allocation.stocks : undefined,
1456
- crypto: item.allocation.crypto !== undefined ? item.allocation.crypto : undefined,
1457
- etfs: item.allocation.etfs !== undefined ? item.allocation.etfs : undefined,
1458
- },
1459
- }
1460
- } : undefined,
1461
- alerts: item.alerts ?
1462
- Array.isArray(item.alerts) && item.alerts.length > 0 && item.alerts.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
1463
- connect: item.alerts.map((item) => ({
1464
- id: item.id
1465
- }))
1466
- } : { upsert: item.alerts.map((item) => ({
1467
- where: {
1468
- id: item.id !== undefined ? item.id : undefined,
1469
- alpacaAccountId: item.alpacaAccountId !== undefined ? {
1470
- equals: item.alpacaAccountId
1471
- } : undefined,
1472
- },
1473
- update: {
1474
- id: item.id !== undefined ? {
1475
- set: item.id
1476
- } : undefined,
1477
- message: item.message !== undefined ? {
1478
- set: item.message
1479
- } : undefined,
1480
- type: item.type !== undefined ? {
1481
- set: item.type
1482
- } : undefined,
1483
- isRead: item.isRead !== undefined ? {
1484
- set: item.isRead
1485
- } : undefined,
1486
- },
1487
- create: {
1488
- message: item.message !== undefined ? item.message : undefined,
1489
- type: item.type !== undefined ? item.type : undefined,
1490
- isRead: item.isRead !== undefined ? item.isRead : undefined,
1491
- },
1492
- }))
1493
- } : undefined,
1494
- },
1495
- create: {
1496
- type: item.type !== undefined ? item.type : undefined,
1497
- APIKey: item.APIKey !== undefined ? item.APIKey : undefined,
1498
- APISecret: item.APISecret !== undefined ? item.APISecret : undefined,
1499
- configuration: item.configuration !== undefined ? item.configuration : undefined,
1500
- marketOpen: item.marketOpen !== undefined ? item.marketOpen : undefined,
1501
- realTime: item.realTime !== undefined ? item.realTime : undefined,
1502
- cryptoTradingEnabled: item.cryptoTradingEnabled !== undefined ? item.cryptoTradingEnabled : undefined,
1503
- cryptoTradingPairs: item.cryptoTradingPairs !== undefined ? {
1504
- set: item.cryptoTradingPairs
1505
- } : undefined,
1506
- cryptoTradeAllocationPct: item.cryptoTradeAllocationPct !== undefined ? item.cryptoTradeAllocationPct : undefined,
1507
- tradeAllocationPct: item.tradeAllocationPct !== undefined ? item.tradeAllocationPct : undefined,
1508
- minPercentageChange: item.minPercentageChange !== undefined ? item.minPercentageChange : undefined,
1509
- volumeThreshold: item.volumeThreshold !== undefined ? item.volumeThreshold : undefined,
1510
- enablePortfolioTrailingStop: item.enablePortfolioTrailingStop !== undefined ? item.enablePortfolioTrailingStop : undefined,
1511
- portfolioTrailPercent: item.portfolioTrailPercent !== undefined ? item.portfolioTrailPercent : undefined,
1512
- portfolioProfitThresholdPercent: item.portfolioProfitThresholdPercent !== undefined ? item.portfolioProfitThresholdPercent : undefined,
1513
- reducedPortfolioTrailPercent: item.reducedPortfolioTrailPercent !== undefined ? item.reducedPortfolioTrailPercent : undefined,
1514
- defaultTrailingStopPercentage100: item.defaultTrailingStopPercentage100 !== undefined ? item.defaultTrailingStopPercentage100 : undefined,
1515
- firstTrailReductionThreshold100: item.firstTrailReductionThreshold100 !== undefined ? item.firstTrailReductionThreshold100 : undefined,
1516
- secondTrailReductionThreshold100: item.secondTrailReductionThreshold100 !== undefined ? item.secondTrailReductionThreshold100 : undefined,
1517
- firstReducedTrailPercentage100: item.firstReducedTrailPercentage100 !== undefined ? item.firstReducedTrailPercentage100 : undefined,
1518
- secondReducedTrailPercentage100: item.secondReducedTrailPercentage100 !== undefined ? item.secondReducedTrailPercentage100 : undefined,
1519
- minimumPriceChangePercent100: item.minimumPriceChangePercent100 !== undefined ? item.minimumPriceChangePercent100 : undefined,
1520
- allocation: item.allocation ?
1521
- typeof item.allocation === 'object' && Object.keys(item.allocation).length === 1 && Object.keys(item.allocation)[0] === 'id'
1522
- ? { connect: {
1523
- id: item.allocation.id
1524
- }
1525
- }
1526
- : { connectOrCreate: {
1527
- where: {
1528
- id: item.allocation.id !== undefined ? item.allocation.id : undefined,
1529
- alpacaAccountId: item.allocation.alpacaAccountId !== undefined ? item.allocation.alpacaAccountId : undefined,
1530
- },
1531
- create: {
1532
- stocks: item.allocation.stocks !== undefined ? item.allocation.stocks : undefined,
1533
- crypto: item.allocation.crypto !== undefined ? item.allocation.crypto : undefined,
1534
- etfs: item.allocation.etfs !== undefined ? item.allocation.etfs : undefined,
1535
- },
1536
- }
2206
+ },
2207
+ create: {
2208
+ type: item.type !== undefined ? item.type : undefined,
2209
+ APIKey: item.APIKey !== undefined ? item.APIKey : undefined,
2210
+ APISecret: item.APISecret !== undefined ? item.APISecret : undefined,
2211
+ configuration: item.configuration !== undefined ? item.configuration : undefined,
2212
+ marketOpen: item.marketOpen !== undefined ? item.marketOpen : undefined,
2213
+ realTime: item.realTime !== undefined ? item.realTime : undefined,
2214
+ cryptoTradingEnabled: item.cryptoTradingEnabled !== undefined ? item.cryptoTradingEnabled : undefined,
2215
+ cryptoTradingPairs: item.cryptoTradingPairs !== undefined ? {
2216
+ set: item.cryptoTradingPairs
1537
2217
  } : undefined,
1538
- alerts: item.alerts ?
1539
- Array.isArray(item.alerts) && item.alerts.length > 0 && item.alerts.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
1540
- connect: item.alerts.map((item) => ({
1541
- id: item.id
1542
- }))
1543
- }
1544
- : { connectOrCreate: item.alerts.map((item) => ({
1545
- where: {
1546
- id: item.id !== undefined ? item.id : undefined,
1547
- alpacaAccountId: item.alpacaAccountId !== undefined ? {
1548
- equals: item.alpacaAccountId
1549
- } : undefined,
1550
- },
1551
- create: {
1552
- message: item.message !== undefined ? item.message : undefined,
1553
- type: item.type !== undefined ? item.type : undefined,
1554
- isRead: item.isRead !== undefined ? item.isRead : undefined,
1555
- },
1556
- }))
1557
- } : undefined,
1558
- },
1559
- }))
1560
- } : undefined,
1561
- },
1562
- };
1563
- const filteredVariables = removeUndefinedProps(variables);
1564
- try {
1565
- const response = await client.mutate({ mutation: UPSERT_ONE_USER, variables: filteredVariables });
1566
- if (response.errors && response.errors.length > 0)
1567
- throw new Error(response.errors[0].message);
1568
- if (response && response.data && response.data.upsertOneUser) {
1569
- return response.data.upsertOneUser;
1570
- }
1571
- else {
1572
- return null;
1573
- }
1574
- }
1575
- catch (error) {
1576
- console.error('Error in upsertOneUser:', error);
1577
- throw error;
1578
- }
1579
- },
1580
- /**
1581
- * Update multiple User records.
1582
- * @param props - Array of User objects for the updated records.
1583
- * @param globalClient - Apollo Client instance.
1584
- * @returns The count of created records or null.
1585
- */
1586
- async updateMany(props, globalClient) {
1587
- const [modules, client] = await Promise.all([
1588
- getApolloModules(),
1589
- globalClient
1590
- ? Promise.resolve(globalClient)
1591
- : importedClient
1592
- ]);
1593
- const { gql, ApolloError } = modules;
1594
- const UPDATE_MANY_USER = gql `
1595
- mutation updateManyUser($data: [UserCreateManyInput!]!) {
1596
- updateManyUser(data: $data) {
1597
- count
1598
- }
1599
- }`;
1600
- const variables = props.map(prop => ({
1601
- where: {
1602
- id: prop.id !== undefined ? prop.id : undefined,
1603
- email: prop.email !== undefined ? prop.email : undefined,
1604
- name: prop.name !== undefined ? {
1605
- equals: prop.name
1606
- } : undefined,
1607
- },
1608
- data: {
1609
- id: prop.id !== undefined ? {
1610
- set: prop.id
1611
- } : undefined,
1612
- name: prop.name !== undefined ? {
1613
- set: prop.name
1614
- } : undefined,
1615
- email: prop.email !== undefined ? {
1616
- set: prop.email
1617
- } : undefined,
1618
- emailVerified: prop.emailVerified !== undefined ? {
1619
- set: prop.emailVerified
1620
- } : undefined,
1621
- image: prop.image !== undefined ? {
1622
- set: prop.image
1623
- } : undefined,
1624
- createdAt: prop.createdAt !== undefined ? {
1625
- set: prop.createdAt
1626
- } : undefined,
1627
- updatedAt: prop.updatedAt !== undefined ? {
1628
- set: prop.updatedAt
1629
- } : undefined,
1630
- role: prop.role !== undefined ? {
1631
- set: prop.role
1632
- } : undefined,
1633
- bio: prop.bio !== undefined ? {
1634
- set: prop.bio
1635
- } : undefined,
1636
- jobTitle: prop.jobTitle !== undefined ? {
1637
- set: prop.jobTitle
1638
- } : undefined,
1639
- currentAccount: prop.currentAccount !== undefined ? {
1640
- set: prop.currentAccount
1641
- } : undefined,
1642
- plan: prop.plan !== undefined ? {
1643
- set: prop.plan
1644
- } : undefined,
1645
- openaiAPIKey: prop.openaiAPIKey !== undefined ? {
1646
- set: prop.openaiAPIKey
1647
- } : undefined,
1648
- openaiModel: prop.openaiModel !== undefined ? {
1649
- set: prop.openaiModel
1650
- } : undefined,
1651
- customer: prop.customer ?
1652
- typeof prop.customer === 'object' && Object.keys(prop.customer).length === 1 && (Object.keys(prop.customer)[0] === 'id' || Object.keys(prop.customer)[0] === 'symbol')
1653
- ? {
1654
- connect: {
1655
- id: prop.customer.id
1656
- }
1657
- } : { upsert: {
1658
- where: {
1659
- id: prop.customer.id !== undefined ? {
1660
- equals: prop.customer.id
1661
- } : undefined,
1662
- authUserId: prop.customer.authUserId !== undefined ? {
1663
- equals: prop.customer.authUserId
1664
- } : undefined,
1665
- name: prop.customer.name !== undefined ? {
1666
- equals: prop.customer.name
1667
- } : undefined,
1668
- stripeCustomerId: prop.customer.stripeCustomerId !== undefined ? {
1669
- equals: prop.customer.stripeCustomerId
1670
- } : undefined,
1671
- stripeSubscriptionId: prop.customer.stripeSubscriptionId !== undefined ? {
1672
- equals: prop.customer.stripeSubscriptionId
1673
- } : undefined,
1674
- stripePriceId: prop.customer.stripePriceId !== undefined ? {
1675
- equals: prop.customer.stripePriceId
1676
- } : undefined,
1677
- },
1678
- update: {
1679
- authUserId: prop.customer.authUserId !== undefined ? {
1680
- set: prop.customer.authUserId
1681
- } : undefined,
1682
- name: prop.customer.name !== undefined ? {
1683
- set: prop.customer.name
1684
- } : undefined,
1685
- plan: prop.customer.plan !== undefined ? {
1686
- set: prop.customer.plan
1687
- } : undefined,
1688
- stripeCustomerId: prop.customer.stripeCustomerId !== undefined ? {
1689
- set: prop.customer.stripeCustomerId
1690
- } : undefined,
1691
- stripeSubscriptionId: prop.customer.stripeSubscriptionId !== undefined ? {
1692
- set: prop.customer.stripeSubscriptionId
1693
- } : undefined,
1694
- stripePriceId: prop.customer.stripePriceId !== undefined ? {
1695
- set: prop.customer.stripePriceId
1696
- } : undefined,
1697
- stripeCurrentPeriodEnd: prop.customer.stripeCurrentPeriodEnd !== undefined ? {
1698
- set: prop.customer.stripeCurrentPeriodEnd
1699
- } : undefined,
1700
- },
1701
- create: {
1702
- authUserId: prop.customer.authUserId !== undefined ? prop.customer.authUserId : undefined,
1703
- name: prop.customer.name !== undefined ? prop.customer.name : undefined,
1704
- plan: prop.customer.plan !== undefined ? prop.customer.plan : undefined,
1705
- stripeCustomerId: prop.customer.stripeCustomerId !== undefined ? prop.customer.stripeCustomerId : undefined,
1706
- stripeSubscriptionId: prop.customer.stripeSubscriptionId !== undefined ? prop.customer.stripeSubscriptionId : undefined,
1707
- stripePriceId: prop.customer.stripePriceId !== undefined ? prop.customer.stripePriceId : undefined,
1708
- stripeCurrentPeriodEnd: prop.customer.stripeCurrentPeriodEnd !== undefined ? prop.customer.stripeCurrentPeriodEnd : undefined,
1709
- },
1710
- }
1711
- } : undefined,
1712
- accounts: prop.accounts ?
1713
- Array.isArray(prop.accounts) && prop.accounts.length > 0 && prop.accounts.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
1714
- connect: prop.accounts.map((item) => ({
1715
- id: item.id
1716
- }))
1717
- } : { upsert: prop.accounts.map((item) => ({
1718
- where: {
1719
- id: item.id !== undefined ? item.id : undefined,
1720
- userId: item.userId !== undefined ? {
1721
- equals: item.userId
1722
- } : undefined,
1723
- providerAccountId: item.providerAccountId !== undefined ? {
1724
- equals: item.providerAccountId
1725
- } : undefined,
1726
- },
1727
- update: {
1728
- id: item.id !== undefined ? {
1729
- set: item.id
1730
- } : undefined,
1731
- type: item.type !== undefined ? {
1732
- set: item.type
1733
- } : undefined,
1734
- provider: item.provider !== undefined ? {
1735
- set: item.provider
1736
- } : undefined,
1737
- providerAccountId: item.providerAccountId !== undefined ? {
1738
- set: item.providerAccountId
1739
- } : undefined,
1740
- refresh_token: item.refresh_token !== undefined ? {
1741
- set: item.refresh_token
1742
- } : undefined,
1743
- access_token: item.access_token !== undefined ? {
1744
- set: item.access_token
1745
- } : undefined,
1746
- expires_at: item.expires_at !== undefined ? {
1747
- set: item.expires_at
1748
- } : undefined,
1749
- token_type: item.token_type !== undefined ? {
1750
- set: item.token_type
1751
- } : undefined,
1752
- scope: item.scope !== undefined ? {
1753
- set: item.scope
1754
- } : undefined,
1755
- id_token: item.id_token !== undefined ? {
1756
- set: item.id_token
1757
- } : undefined,
1758
- session_state: item.session_state !== undefined ? {
1759
- set: item.session_state
1760
- } : undefined,
1761
- },
1762
- create: {
1763
- type: item.type !== undefined ? item.type : undefined,
1764
- provider: item.provider !== undefined ? item.provider : undefined,
1765
- providerAccountId: item.providerAccountId !== undefined ? item.providerAccountId : undefined,
1766
- refresh_token: item.refresh_token !== undefined ? item.refresh_token : undefined,
1767
- access_token: item.access_token !== undefined ? item.access_token : undefined,
1768
- expires_at: item.expires_at !== undefined ? item.expires_at : undefined,
1769
- token_type: item.token_type !== undefined ? item.token_type : undefined,
1770
- scope: item.scope !== undefined ? item.scope : undefined,
1771
- id_token: item.id_token !== undefined ? item.id_token : undefined,
1772
- session_state: item.session_state !== undefined ? item.session_state : undefined,
1773
- },
1774
- }))
1775
- } : undefined,
1776
- sessions: prop.sessions ?
1777
- Array.isArray(prop.sessions) && prop.sessions.length > 0 && prop.sessions.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
1778
- connect: prop.sessions.map((item) => ({
1779
- id: item.id
1780
- }))
1781
- } : { upsert: prop.sessions.map((item) => ({
1782
- where: {
1783
- id: item.id !== undefined ? item.id : undefined,
1784
- userId: item.userId !== undefined ? {
1785
- equals: item.userId
1786
- } : undefined,
1787
- },
1788
- update: {
1789
- id: item.id !== undefined ? {
1790
- set: item.id
1791
- } : undefined,
1792
- sessionToken: item.sessionToken !== undefined ? {
1793
- set: item.sessionToken
1794
- } : undefined,
1795
- expires: item.expires !== undefined ? {
1796
- set: item.expires
1797
- } : undefined,
1798
- },
1799
- create: {
1800
- sessionToken: item.sessionToken !== undefined ? item.sessionToken : undefined,
1801
- expires: item.expires !== undefined ? item.expires : undefined,
1802
- },
1803
- }))
1804
- } : undefined,
1805
- authenticators: prop.authenticators ?
1806
- Array.isArray(prop.authenticators) && prop.authenticators.length > 0 && prop.authenticators.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
1807
- connect: prop.authenticators.map((item) => ({
1808
- id: item.id
1809
- }))
1810
- } : { upsert: prop.authenticators.map((item) => ({
1811
- where: {
1812
- id: item.id !== undefined ? item.id : undefined,
1813
- userId: item.userId !== undefined ? {
1814
- equals: item.userId
1815
- } : undefined,
1816
- },
1817
- update: {
1818
- id: item.id !== undefined ? {
1819
- set: item.id
1820
- } : undefined,
1821
- credentialID: item.credentialID !== undefined ? {
1822
- set: item.credentialID
1823
- } : undefined,
1824
- publicKey: item.publicKey !== undefined ? {
1825
- set: item.publicKey
1826
- } : undefined,
1827
- counter: item.counter !== undefined ? {
1828
- set: item.counter
1829
- } : undefined,
1830
- },
1831
- create: {
1832
- credentialID: item.credentialID !== undefined ? item.credentialID : undefined,
1833
- publicKey: item.publicKey !== undefined ? item.publicKey : undefined,
1834
- counter: item.counter !== undefined ? item.counter : undefined,
1835
- },
1836
- }))
1837
- } : undefined,
1838
- alpacaAccounts: prop.alpacaAccounts ?
1839
- Array.isArray(prop.alpacaAccounts) && prop.alpacaAccounts.length > 0 && prop.alpacaAccounts.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
1840
- connect: prop.alpacaAccounts.map((item) => ({
1841
- id: item.id
1842
- }))
1843
- } : { upsert: prop.alpacaAccounts.map((item) => ({
1844
- where: {
1845
- id: item.id !== undefined ? item.id : undefined,
1846
- userId: item.userId !== undefined ? {
1847
- equals: item.userId
1848
- } : undefined,
1849
- },
1850
- update: {
1851
- id: item.id !== undefined ? {
1852
- set: item.id
1853
- } : undefined,
1854
- type: item.type !== undefined ? {
1855
- set: item.type
1856
- } : undefined,
1857
- APIKey: item.APIKey !== undefined ? {
1858
- set: item.APIKey
1859
- } : undefined,
1860
- APISecret: item.APISecret !== undefined ? {
1861
- set: item.APISecret
1862
- } : undefined,
1863
- configuration: item.configuration !== undefined ? {
1864
- set: item.configuration
1865
- } : undefined,
1866
- marketOpen: item.marketOpen !== undefined ? {
1867
- set: item.marketOpen
1868
- } : undefined,
1869
- realTime: item.realTime !== undefined ? {
1870
- set: item.realTime
1871
- } : undefined,
1872
- cryptoTradingEnabled: item.cryptoTradingEnabled !== undefined ? {
1873
- set: item.cryptoTradingEnabled
1874
- } : undefined,
1875
- cryptoTradingPairs: item.cryptoTradingPairs !== undefined ? {
1876
- set: item.cryptoTradingPairs
1877
- } : undefined,
1878
- cryptoTradeAllocationPct: item.cryptoTradeAllocationPct !== undefined ? {
1879
- set: item.cryptoTradeAllocationPct
1880
- } : undefined,
1881
- tradeAllocationPct: item.tradeAllocationPct !== undefined ? {
1882
- set: item.tradeAllocationPct
1883
- } : undefined,
1884
- minPercentageChange: item.minPercentageChange !== undefined ? {
1885
- set: item.minPercentageChange
1886
- } : undefined,
1887
- volumeThreshold: item.volumeThreshold !== undefined ? {
1888
- set: item.volumeThreshold
1889
- } : undefined,
1890
- enablePortfolioTrailingStop: item.enablePortfolioTrailingStop !== undefined ? {
1891
- set: item.enablePortfolioTrailingStop
1892
- } : undefined,
1893
- portfolioTrailPercent: item.portfolioTrailPercent !== undefined ? {
1894
- set: item.portfolioTrailPercent
1895
- } : undefined,
1896
- portfolioProfitThresholdPercent: item.portfolioProfitThresholdPercent !== undefined ? {
1897
- set: item.portfolioProfitThresholdPercent
1898
- } : undefined,
1899
- reducedPortfolioTrailPercent: item.reducedPortfolioTrailPercent !== undefined ? {
1900
- set: item.reducedPortfolioTrailPercent
1901
- } : undefined,
1902
- defaultTrailingStopPercentage100: item.defaultTrailingStopPercentage100 !== undefined ? {
1903
- set: item.defaultTrailingStopPercentage100
1904
- } : undefined,
1905
- firstTrailReductionThreshold100: item.firstTrailReductionThreshold100 !== undefined ? {
1906
- set: item.firstTrailReductionThreshold100
1907
- } : undefined,
1908
- secondTrailReductionThreshold100: item.secondTrailReductionThreshold100 !== undefined ? {
1909
- set: item.secondTrailReductionThreshold100
1910
- } : undefined,
1911
- firstReducedTrailPercentage100: item.firstReducedTrailPercentage100 !== undefined ? {
1912
- set: item.firstReducedTrailPercentage100
1913
- } : undefined,
1914
- secondReducedTrailPercentage100: item.secondReducedTrailPercentage100 !== undefined ? {
1915
- set: item.secondReducedTrailPercentage100
1916
- } : undefined,
1917
- minimumPriceChangePercent100: item.minimumPriceChangePercent100 !== undefined ? {
1918
- set: item.minimumPriceChangePercent100
1919
- } : undefined,
1920
- allocation: item.allocation ?
1921
- typeof item.allocation === 'object' && Object.keys(item.allocation).length === 1 && (Object.keys(item.allocation)[0] === 'id' || Object.keys(item.allocation)[0] === 'symbol')
1922
- ? {
1923
- connect: {
1924
- id: item.allocation.id
1925
- }
1926
- } : { upsert: {
1927
- where: {
1928
- id: item.allocation.id !== undefined ? {
1929
- equals: item.allocation.id
1930
- } : undefined,
1931
- alpacaAccountId: item.allocation.alpacaAccountId !== undefined ? {
1932
- equals: item.allocation.alpacaAccountId
1933
- } : undefined,
1934
- },
1935
- update: {
1936
- id: item.allocation.id !== undefined ? {
1937
- set: item.allocation.id
1938
- } : undefined,
1939
- stocks: item.allocation.stocks !== undefined ? {
1940
- set: item.allocation.stocks
1941
- } : undefined,
1942
- crypto: item.allocation.crypto !== undefined ? {
1943
- set: item.allocation.crypto
1944
- } : undefined,
1945
- etfs: item.allocation.etfs !== undefined ? {
1946
- set: item.allocation.etfs
1947
- } : undefined,
1948
- },
1949
- create: {
1950
- stocks: item.allocation.stocks !== undefined ? item.allocation.stocks : undefined,
1951
- crypto: item.allocation.crypto !== undefined ? item.allocation.crypto : undefined,
1952
- etfs: item.allocation.etfs !== undefined ? item.allocation.etfs : undefined,
1953
- },
1954
- }
1955
- } : undefined,
1956
- alerts: item.alerts ?
1957
- Array.isArray(item.alerts) && item.alerts.length > 0 && item.alerts.every((item) => typeof item === 'object' && ('id' in item || 'symbol' in item) && Object.keys(item).length === 1) ? {
1958
- connect: item.alerts.map((item) => ({
1959
- id: item.id
1960
- }))
1961
- } : { upsert: item.alerts.map((item) => ({
1962
- where: {
1963
- id: item.id !== undefined ? item.id : undefined,
1964
- alpacaAccountId: item.alpacaAccountId !== undefined ? {
1965
- equals: item.alpacaAccountId
1966
- } : undefined,
1967
- },
1968
- update: {
1969
- id: item.id !== undefined ? {
1970
- set: item.id
1971
- } : undefined,
1972
- message: item.message !== undefined ? {
1973
- set: item.message
1974
- } : undefined,
1975
- type: item.type !== undefined ? {
1976
- set: item.type
1977
- } : undefined,
1978
- isRead: item.isRead !== undefined ? {
1979
- set: item.isRead
2218
+ cryptoTradeAllocationPct: item.cryptoTradeAllocationPct !== undefined ? item.cryptoTradeAllocationPct : undefined,
2219
+ tradeAllocationPct: item.tradeAllocationPct !== undefined ? item.tradeAllocationPct : undefined,
2220
+ minPercentageChange: item.minPercentageChange !== undefined ? item.minPercentageChange : undefined,
2221
+ volumeThreshold: item.volumeThreshold !== undefined ? item.volumeThreshold : undefined,
2222
+ enablePortfolioTrailingStop: item.enablePortfolioTrailingStop !== undefined ? item.enablePortfolioTrailingStop : undefined,
2223
+ portfolioTrailPercent: item.portfolioTrailPercent !== undefined ? item.portfolioTrailPercent : undefined,
2224
+ portfolioProfitThresholdPercent: item.portfolioProfitThresholdPercent !== undefined ? item.portfolioProfitThresholdPercent : undefined,
2225
+ reducedPortfolioTrailPercent: item.reducedPortfolioTrailPercent !== undefined ? item.reducedPortfolioTrailPercent : undefined,
2226
+ defaultTrailingStopPercentage100: item.defaultTrailingStopPercentage100 !== undefined ? item.defaultTrailingStopPercentage100 : undefined,
2227
+ firstTrailReductionThreshold100: item.firstTrailReductionThreshold100 !== undefined ? item.firstTrailReductionThreshold100 : undefined,
2228
+ secondTrailReductionThreshold100: item.secondTrailReductionThreshold100 !== undefined ? item.secondTrailReductionThreshold100 : undefined,
2229
+ firstReducedTrailPercentage100: item.firstReducedTrailPercentage100 !== undefined ? item.firstReducedTrailPercentage100 : undefined,
2230
+ secondReducedTrailPercentage100: item.secondReducedTrailPercentage100 !== undefined ? item.secondReducedTrailPercentage100 : undefined,
2231
+ minimumPriceChangePercent100: item.minimumPriceChangePercent100 !== undefined ? item.minimumPriceChangePercent100 : undefined,
2232
+ allocation: item.allocation ?
2233
+ typeof item.allocation === 'object' && Object.keys(item.allocation).length === 1 && Object.keys(item.allocation)[0] === 'id'
2234
+ ? { connect: {
2235
+ id: item.allocation.id
2236
+ }
2237
+ }
2238
+ : { connectOrCreate: {
2239
+ where: {
2240
+ id: item.allocation.id !== undefined ? item.allocation.id : undefined,
2241
+ alpacaAccountId: item.allocation.alpacaAccountId !== undefined ? item.allocation.alpacaAccountId : undefined,
2242
+ },
2243
+ create: {
2244
+ stocks: item.allocation.stocks !== undefined ? item.allocation.stocks : undefined,
2245
+ crypto: item.allocation.crypto !== undefined ? item.allocation.crypto : undefined,
2246
+ etfs: item.allocation.etfs !== undefined ? item.allocation.etfs : undefined,
2247
+ },
2248
+ }
1980
2249
  } : undefined,
1981
- },
1982
- create: {
1983
- message: item.message !== undefined ? item.message : undefined,
1984
- type: item.type !== undefined ? item.type : undefined,
1985
- isRead: item.isRead !== undefined ? item.isRead : undefined,
1986
- },
1987
- }))
1988
- } : undefined,
1989
- },
1990
- create: {
1991
- type: item.type !== undefined ? item.type : undefined,
1992
- APIKey: item.APIKey !== undefined ? item.APIKey : undefined,
1993
- APISecret: item.APISecret !== undefined ? item.APISecret : undefined,
1994
- configuration: item.configuration !== undefined ? item.configuration : undefined,
1995
- marketOpen: item.marketOpen !== undefined ? item.marketOpen : undefined,
1996
- realTime: item.realTime !== undefined ? item.realTime : undefined,
1997
- cryptoTradingEnabled: item.cryptoTradingEnabled !== undefined ? item.cryptoTradingEnabled : undefined,
1998
- cryptoTradingPairs: item.cryptoTradingPairs !== undefined ? {
1999
- set: item.cryptoTradingPairs
2000
- } : undefined,
2001
- cryptoTradeAllocationPct: item.cryptoTradeAllocationPct !== undefined ? item.cryptoTradeAllocationPct : undefined,
2002
- tradeAllocationPct: item.tradeAllocationPct !== undefined ? item.tradeAllocationPct : undefined,
2003
- minPercentageChange: item.minPercentageChange !== undefined ? item.minPercentageChange : undefined,
2004
- volumeThreshold: item.volumeThreshold !== undefined ? item.volumeThreshold : undefined,
2005
- enablePortfolioTrailingStop: item.enablePortfolioTrailingStop !== undefined ? item.enablePortfolioTrailingStop : undefined,
2006
- portfolioTrailPercent: item.portfolioTrailPercent !== undefined ? item.portfolioTrailPercent : undefined,
2007
- portfolioProfitThresholdPercent: item.portfolioProfitThresholdPercent !== undefined ? item.portfolioProfitThresholdPercent : undefined,
2008
- reducedPortfolioTrailPercent: item.reducedPortfolioTrailPercent !== undefined ? item.reducedPortfolioTrailPercent : undefined,
2009
- defaultTrailingStopPercentage100: item.defaultTrailingStopPercentage100 !== undefined ? item.defaultTrailingStopPercentage100 : undefined,
2010
- firstTrailReductionThreshold100: item.firstTrailReductionThreshold100 !== undefined ? item.firstTrailReductionThreshold100 : undefined,
2011
- secondTrailReductionThreshold100: item.secondTrailReductionThreshold100 !== undefined ? item.secondTrailReductionThreshold100 : undefined,
2012
- firstReducedTrailPercentage100: item.firstReducedTrailPercentage100 !== undefined ? item.firstReducedTrailPercentage100 : undefined,
2013
- secondReducedTrailPercentage100: item.secondReducedTrailPercentage100 !== undefined ? item.secondReducedTrailPercentage100 : undefined,
2014
- minimumPriceChangePercent100: item.minimumPriceChangePercent100 !== undefined ? item.minimumPriceChangePercent100 : undefined,
2015
- allocation: item.allocation ?
2016
- typeof item.allocation === 'object' && Object.keys(item.allocation).length === 1 && Object.keys(item.allocation)[0] === 'id'
2017
- ? { connect: {
2018
- id: item.allocation.id
2019
- }
2020
- }
2021
- : { connectOrCreate: {
2022
- where: {
2023
- id: item.allocation.id !== undefined ? item.allocation.id : undefined,
2024
- alpacaAccountId: item.allocation.alpacaAccountId !== undefined ? item.allocation.alpacaAccountId : undefined,
2025
- },
2026
- create: {
2027
- stocks: item.allocation.stocks !== undefined ? item.allocation.stocks : undefined,
2028
- crypto: item.allocation.crypto !== undefined ? item.allocation.crypto : undefined,
2029
- etfs: item.allocation.etfs !== undefined ? item.allocation.etfs : undefined,
2030
- },
2250
+ alerts: item.alerts ?
2251
+ Array.isArray(item.alerts) && item.alerts.length > 0 && item.alerts.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
2252
+ connect: item.alerts.map((item) => ({
2253
+ id: item.id
2254
+ }))
2031
2255
  }
2032
- } : undefined,
2033
- alerts: item.alerts ?
2034
- Array.isArray(item.alerts) && item.alerts.length > 0 && item.alerts.every((item) => typeof item === 'object' && 'id' in item && Object.keys(item).length === 1) ? {
2035
- connect: item.alerts.map((item) => ({
2036
- id: item.id
2037
- }))
2038
- }
2039
- : { connectOrCreate: item.alerts.map((item) => ({
2040
- where: {
2041
- id: item.id !== undefined ? item.id : undefined,
2042
- alpacaAccountId: item.alpacaAccountId !== undefined ? {
2043
- equals: item.alpacaAccountId
2044
- } : undefined,
2045
- },
2046
- create: {
2047
- message: item.message !== undefined ? item.message : undefined,
2048
- type: item.type !== undefined ? item.type : undefined,
2049
- isRead: item.isRead !== undefined ? item.isRead : undefined,
2050
- },
2051
- }))
2052
- } : undefined,
2053
- },
2054
- }))
2055
- } : undefined,
2056
- },
2057
- }));
2058
- const filteredVariables = removeUndefinedProps(variables);
2059
- try {
2060
- const response = await client.mutate({ mutation: UPDATE_MANY_USER, variables: filteredVariables });
2061
- if (response.errors && response.errors.length > 0)
2062
- throw new Error(response.errors[0].message);
2063
- if (response && response.data && response.data.updateManyUser) {
2064
- return response.data.updateManyUser;
2256
+ : { connectOrCreate: item.alerts.map((item) => ({
2257
+ where: {
2258
+ id: item.id !== undefined ? item.id : undefined,
2259
+ alpacaAccountId: item.alpacaAccountId !== undefined ? {
2260
+ equals: item.alpacaAccountId
2261
+ } : undefined,
2262
+ },
2263
+ create: {
2264
+ message: item.message !== undefined ? item.message : undefined,
2265
+ type: item.type !== undefined ? item.type : undefined,
2266
+ isRead: item.isRead !== undefined ? item.isRead : undefined,
2267
+ },
2268
+ }))
2269
+ } : undefined,
2270
+ },
2271
+ }))
2272
+ } : undefined,
2273
+ },
2274
+ }));
2275
+ const filteredVariables = removeUndefinedProps(variables);
2276
+ const response = await client.mutate({
2277
+ mutation: UPDATE_MANY_USER,
2278
+ variables: filteredVariables,
2279
+ // Don't cache mutations, but ensure we're using the freshest context
2280
+ fetchPolicy: 'no-cache'
2281
+ });
2282
+ if (response.errors && response.errors.length > 0)
2283
+ throw new Error(response.errors[0].message);
2284
+ if (response && response.data && response.data.updateManyUser) {
2285
+ return response.data.updateManyUser;
2286
+ }
2287
+ else {
2288
+ return null;
2289
+ }
2065
2290
  }
2066
- else {
2067
- return null;
2291
+ catch (error) {
2292
+ lastError = error;
2293
+ // Check if this is a database connection error that we should retry
2294
+ const isConnectionError = error.message?.includes('Server has closed the connection') ||
2295
+ error.message?.includes('Cannot reach database server') ||
2296
+ error.message?.includes('Connection timed out') ||
2297
+ error.message?.includes('Accelerate') || // Prisma Accelerate proxy errors
2298
+ (error.networkError && error.networkError.message?.includes('Failed to fetch'));
2299
+ if (isConnectionError && retryCount < MAX_RETRIES - 1) {
2300
+ retryCount++;
2301
+ const delay = Math.pow(2, retryCount) * 100; // Exponential backoff: 200ms, 400ms, 800ms
2302
+ console.warn("Database connection error, retrying...");
2303
+ await new Promise(resolve => setTimeout(resolve, delay));
2304
+ continue;
2305
+ }
2306
+ // Log the error and rethrow
2307
+ console.error("Database error occurred:", error);
2308
+ throw error;
2068
2309
  }
2069
2310
  }
2070
- catch (error) {
2071
- console.error('Error in updateManyUser:', error);
2072
- throw error;
2073
- }
2311
+ // If we exhausted retries, throw the last error
2312
+ throw lastError;
2074
2313
  },
2075
2314
  /**
2076
2315
  * Delete a single User record.
2077
- * @param props - Properties to update.
2316
+ * Enhanced with connection resilience against Prisma connection errors.
2317
+ * @param props - Properties to identify the record to delete.
2078
2318
  * @param globalClient - Apollo Client instance.
2079
2319
  * @returns The deleted User or null.
2080
2320
  */
2081
2321
  async delete(props, globalClient) {
2082
- const [modules, client] = await Promise.all([
2083
- getApolloModules(),
2084
- globalClient
2085
- ? Promise.resolve(globalClient)
2086
- : importedClient
2087
- ]);
2088
- const { gql, ApolloError } = modules;
2089
- const DELETE_ONE_USER = gql `
2090
- mutation deleteOneUser($where: UserWhereUniqueInput!) {
2091
- deleteOneUser(where: $where) {
2092
- id
2093
- }
2094
- }`;
2095
- const variables = {
2096
- where: {
2097
- id: props.id ? props.id : undefined,
2322
+ // Maximum number of retries for database connection issues
2323
+ const MAX_RETRIES = 3;
2324
+ let retryCount = 0;
2325
+ let lastError = null;
2326
+ // Retry loop to handle potential database connection issues
2327
+ while (retryCount < MAX_RETRIES) {
2328
+ try {
2329
+ const [modules, client] = await Promise.all([
2330
+ getApolloModules(),
2331
+ globalClient
2332
+ ? Promise.resolve(globalClient)
2333
+ : importedClient
2334
+ ]);
2335
+ const { gql, ApolloError } = modules;
2336
+ const DELETE_ONE_USER = gql `
2337
+ mutation deleteOneUser($where: UserWhereUniqueInput!) {
2338
+ deleteOneUser(where: $where) {
2339
+ id
2098
2340
  }
2099
- };
2100
- const filteredVariables = removeUndefinedProps(variables);
2101
- try {
2102
- const response = await client.mutate({ mutation: DELETE_ONE_USER, variables: filteredVariables });
2103
- if (response.errors && response.errors.length > 0)
2104
- throw new Error(response.errors[0].message);
2105
- if (response && response.data && response.data.deleteOneUser) {
2106
- return response.data.deleteOneUser;
2341
+ }`;
2342
+ const variables = {
2343
+ where: {
2344
+ id: props.id ? props.id : undefined,
2345
+ }
2346
+ };
2347
+ const filteredVariables = removeUndefinedProps(variables);
2348
+ const response = await client.mutate({
2349
+ mutation: DELETE_ONE_USER,
2350
+ variables: filteredVariables,
2351
+ // Don't cache mutations, but ensure we're using the freshest context
2352
+ fetchPolicy: 'no-cache'
2353
+ });
2354
+ if (response.errors && response.errors.length > 0)
2355
+ throw new Error(response.errors[0].message);
2356
+ if (response && response.data && response.data.deleteOneUser) {
2357
+ return response.data.deleteOneUser;
2358
+ }
2359
+ else {
2360
+ return null;
2361
+ }
2107
2362
  }
2108
- else {
2109
- return null;
2363
+ catch (error) {
2364
+ lastError = error;
2365
+ // Check if this is a database connection error that we should retry
2366
+ const isConnectionError = error.message?.includes('Server has closed the connection') ||
2367
+ error.message?.includes('Cannot reach database server') ||
2368
+ error.message?.includes('Connection timed out') ||
2369
+ error.message?.includes('Accelerate') || // Prisma Accelerate proxy errors
2370
+ (error.networkError && error.networkError.message?.includes('Failed to fetch'));
2371
+ if (isConnectionError && retryCount < MAX_RETRIES - 1) {
2372
+ retryCount++;
2373
+ const delay = Math.pow(2, retryCount) * 100; // Exponential backoff: 200ms, 400ms, 800ms
2374
+ console.warn("Database connection error, retrying...");
2375
+ await new Promise(resolve => setTimeout(resolve, delay));
2376
+ continue;
2377
+ }
2378
+ // Log the error and rethrow
2379
+ console.error("Database error occurred:", error);
2380
+ throw error;
2110
2381
  }
2111
2382
  }
2112
- catch (error) {
2113
- console.error('Error in deleteOneUser:', error);
2114
- throw error;
2115
- }
2383
+ // If we exhausted retries, throw the last error
2384
+ throw lastError;
2116
2385
  },
2117
2386
  /**
2118
2387
  * Retrieve a single User record by ID.
2119
- * @param props - Properties to update.
2388
+ * Enhanced with connection resilience against Prisma connection errors.
2389
+ * @param props - Properties to identify the record.
2120
2390
  * @param globalClient - Apollo Client instance.
2391
+ * @param whereInput - Optional custom where input.
2121
2392
  * @returns The retrieved User or null.
2122
2393
  */
2123
2394
  async get(props, globalClient, whereInput) {
2124
- const [modules, client] = await Promise.all([
2125
- getApolloModules(),
2126
- globalClient
2127
- ? Promise.resolve(globalClient)
2128
- : importedClient
2129
- ]);
2130
- const { gql, ApolloError } = modules;
2131
- const GET_USER = gql `
2132
- query getUser($where: UserWhereUniqueInput!) {
2133
- getUser(where: $where) {
2134
- ${selectionSet}
2135
- }
2136
- }`;
2137
- const variables = {
2138
- where: whereInput ? whereInput : {
2139
- id: props.id !== undefined ? props.id : undefined,
2140
- email: props.email !== undefined ? props.email : undefined,
2141
- name: props.name !== undefined ? {
2142
- equals: props.name
2143
- } : undefined,
2144
- },
2145
- };
2146
- const filteredVariables = removeUndefinedProps(variables);
2147
- try {
2148
- const response = await client.query({ query: GET_USER, variables: filteredVariables });
2149
- if (response.errors && response.errors.length > 0)
2150
- throw new Error(response.errors[0].message);
2151
- return response.data?.getUser ?? null;
2152
- }
2153
- catch (error) {
2154
- if (error instanceof ApolloError && error.message === 'No User found') {
2155
- return null;
2395
+ // Maximum number of retries for database connection issues
2396
+ const MAX_RETRIES = 3;
2397
+ let retryCount = 0;
2398
+ let lastError = null;
2399
+ // Retry loop to handle potential database connection issues
2400
+ while (retryCount < MAX_RETRIES) {
2401
+ try {
2402
+ const [modules, client] = await Promise.all([
2403
+ getApolloModules(),
2404
+ globalClient
2405
+ ? Promise.resolve(globalClient)
2406
+ : importedClient
2407
+ ]);
2408
+ const { gql, ApolloError } = modules;
2409
+ const GET_USER = gql `
2410
+ query getUser($where: UserWhereUniqueInput!) {
2411
+ getUser(where: $where) {
2412
+ ${selectionSet}
2413
+ }
2414
+ }`;
2415
+ const variables = {
2416
+ where: whereInput ? whereInput : {
2417
+ id: props.id !== undefined ? props.id : undefined,
2418
+ email: props.email !== undefined ? props.email : undefined,
2419
+ name: props.name !== undefined ? props.name : undefined,
2420
+ emailVerified: props.emailVerified !== undefined ? {
2421
+ equals: props.emailVerified
2422
+ } : undefined,
2423
+ image: props.image !== undefined ? {
2424
+ equals: props.image
2425
+ } : undefined,
2426
+ createdAt: props.createdAt !== undefined ? {
2427
+ equals: props.createdAt
2428
+ } : undefined,
2429
+ updatedAt: props.updatedAt !== undefined ? {
2430
+ equals: props.updatedAt
2431
+ } : undefined,
2432
+ role: props.role !== undefined ? {
2433
+ equals: props.role
2434
+ } : undefined,
2435
+ bio: props.bio !== undefined ? {
2436
+ equals: props.bio
2437
+ } : undefined,
2438
+ jobTitle: props.jobTitle !== undefined ? {
2439
+ equals: props.jobTitle
2440
+ } : undefined,
2441
+ currentAccount: props.currentAccount !== undefined ? {
2442
+ equals: props.currentAccount
2443
+ } : undefined,
2444
+ plan: props.plan !== undefined ? {
2445
+ equals: props.plan
2446
+ } : undefined,
2447
+ openaiAPIKey: props.openaiAPIKey !== undefined ? {
2448
+ equals: props.openaiAPIKey
2449
+ } : undefined,
2450
+ openaiModel: props.openaiModel !== undefined ? {
2451
+ equals: props.openaiModel
2452
+ } : undefined,
2453
+ },
2454
+ };
2455
+ const filteredVariables = removeUndefinedProps(variables);
2456
+ const response = await client.query({
2457
+ query: GET_USER,
2458
+ variables: filteredVariables,
2459
+ fetchPolicy: 'network-only', // Force network request to avoid stale cache
2460
+ });
2461
+ if (response.errors && response.errors.length > 0)
2462
+ throw new Error(response.errors[0].message);
2463
+ return response.data?.getUser ?? null;
2156
2464
  }
2157
- else {
2158
- console.error('Error in getUser:', error);
2465
+ catch (error) {
2466
+ lastError = error;
2467
+ // Check if this is a "No record found" error - this is an expected condition, not a failure
2468
+ if (error.message === 'No User found') {
2469
+ return null;
2470
+ }
2471
+ // Check if this is a database connection error that we should retry
2472
+ const isConnectionError = error.message?.includes('Server has closed the connection') ||
2473
+ error.message?.includes('Cannot reach database server') ||
2474
+ error.message?.includes('Connection timed out') ||
2475
+ error.message?.includes('Accelerate') || // Prisma Accelerate proxy errors
2476
+ (error.networkError && error.networkError.message?.includes('Failed to fetch'));
2477
+ if (isConnectionError && retryCount < MAX_RETRIES - 1) {
2478
+ retryCount++;
2479
+ const delay = Math.pow(2, retryCount) * 100; // Exponential backoff: 200ms, 400ms, 800ms
2480
+ console.warn("Database connection error, retrying...");
2481
+ await new Promise(resolve => setTimeout(resolve, delay));
2482
+ continue;
2483
+ }
2484
+ // Log the error and rethrow
2485
+ console.error("Database error occurred:", error);
2159
2486
  throw error;
2160
2487
  }
2161
2488
  }
2489
+ // If we exhausted retries, throw the last error
2490
+ throw lastError;
2162
2491
  },
2163
2492
  /**
2164
2493
  * Retrieve all Users records.
2494
+ * Enhanced with connection resilience against Prisma connection errors.
2165
2495
  * @param globalClient - Apollo Client instance.
2166
2496
  * @returns An array of User records or null.
2167
2497
  */
2168
2498
  async getAll(globalClient) {
2169
- const [modules, client] = await Promise.all([
2170
- getApolloModules(),
2171
- globalClient
2172
- ? Promise.resolve(globalClient)
2173
- : importedClient
2174
- ]);
2175
- const { gql, ApolloError } = modules;
2176
- const GET_ALL_USER = gql `
2177
- query getAllUser {
2178
- users {
2179
- ${selectionSet}
2180
- }
2181
- }`;
2182
- try {
2183
- const response = await client.query({ query: GET_ALL_USER });
2184
- if (response.errors && response.errors.length > 0)
2185
- throw new Error(response.errors[0].message);
2186
- return response.data?.users ?? null;
2187
- }
2188
- catch (error) {
2189
- if (error instanceof ApolloError && error.message === 'No User found') {
2190
- return null;
2499
+ // Maximum number of retries for database connection issues
2500
+ const MAX_RETRIES = 3;
2501
+ let retryCount = 0;
2502
+ let lastError = null;
2503
+ // Retry loop to handle potential database connection issues
2504
+ while (retryCount < MAX_RETRIES) {
2505
+ try {
2506
+ const [modules, client] = await Promise.all([
2507
+ getApolloModules(),
2508
+ globalClient
2509
+ ? Promise.resolve(globalClient)
2510
+ : importedClient
2511
+ ]);
2512
+ const { gql, ApolloError } = modules;
2513
+ const GET_ALL_USER = gql `
2514
+ query getAllUser {
2515
+ users {
2516
+ ${selectionSet}
2517
+ }
2518
+ }`;
2519
+ const response = await client.query({
2520
+ query: GET_ALL_USER,
2521
+ fetchPolicy: 'network-only', // Force network request to avoid stale cache
2522
+ });
2523
+ if (response.errors && response.errors.length > 0)
2524
+ throw new Error(response.errors[0].message);
2525
+ return response.data?.users ?? null;
2191
2526
  }
2192
- else {
2193
- console.error('Error in getUser:', error);
2527
+ catch (error) {
2528
+ lastError = error;
2529
+ // Check if this is a "No record found" error - this is an expected condition, not a failure
2530
+ if (error.message === 'No User found') {
2531
+ return null;
2532
+ }
2533
+ // Check if this is a database connection error that we should retry
2534
+ const isConnectionError = error.message?.includes('Server has closed the connection') ||
2535
+ error.message?.includes('Cannot reach database server') ||
2536
+ error.message?.includes('Connection timed out') ||
2537
+ error.message?.includes('Accelerate') || // Prisma Accelerate proxy errors
2538
+ (error.networkError && error.networkError.message?.includes('Failed to fetch'));
2539
+ if (isConnectionError && retryCount < MAX_RETRIES - 1) {
2540
+ retryCount++;
2541
+ const delay = Math.pow(2, retryCount) * 100; // Exponential backoff: 200ms, 400ms, 800ms
2542
+ console.warn("Database connection error, retrying...");
2543
+ await new Promise(resolve => setTimeout(resolve, delay));
2544
+ continue;
2545
+ }
2546
+ // Log the error and rethrow
2547
+ console.error("Database error occurred:", error);
2194
2548
  throw error;
2195
2549
  }
2196
2550
  }
2551
+ // If we exhausted retries, throw the last error
2552
+ throw lastError;
2197
2553
  },
2198
2554
  /**
2199
2555
  * Find multiple User records based on conditions.
2556
+ * Enhanced with connection resilience against Prisma connection errors.
2200
2557
  * @param props - Conditions to find records.
2201
2558
  * @param globalClient - Apollo Client instance.
2559
+ * @param whereInput - Optional custom where input.
2202
2560
  * @returns An array of found User records or null.
2203
2561
  */
2204
2562
  async findMany(props, globalClient, whereInput) {
2205
- const [modules, client] = await Promise.all([
2206
- getApolloModules(),
2207
- globalClient
2208
- ? Promise.resolve(globalClient)
2209
- : importedClient
2210
- ]);
2211
- const { gql, ApolloError } = modules;
2212
- const FIND_MANY_USER = gql `
2213
- query findManyUser($where: UserWhereInput!) {
2214
- users(where: $where) {
2215
- ${selectionSet}
2216
- }
2217
- }`;
2218
- const variables = {
2219
- where: whereInput ? whereInput : {
2220
- id: props.id !== undefined ? {
2221
- equals: props.id
2222
- } : undefined,
2223
- name: props.name !== undefined ? {
2224
- equals: props.name
2225
- } : undefined,
2226
- email: props.email !== undefined ? {
2227
- equals: props.email
2228
- } : undefined,
2229
- },
2230
- };
2231
- const filteredVariables = removeUndefinedProps(variables);
2232
- try {
2233
- const response = await client.query({ query: FIND_MANY_USER, variables: filteredVariables });
2234
- if (response.errors && response.errors.length > 0)
2235
- throw new Error(response.errors[0].message);
2236
- if (response && response.data && response.data.users) {
2237
- return response.data.users;
2238
- }
2239
- else {
2240
- return [];
2563
+ // Maximum number of retries for database connection issues
2564
+ const MAX_RETRIES = 3;
2565
+ let retryCount = 0;
2566
+ let lastError = null;
2567
+ // Retry loop to handle potential database connection issues
2568
+ while (retryCount < MAX_RETRIES) {
2569
+ try {
2570
+ const [modules, client] = await Promise.all([
2571
+ getApolloModules(),
2572
+ globalClient
2573
+ ? Promise.resolve(globalClient)
2574
+ : importedClient
2575
+ ]);
2576
+ const { gql, ApolloError } = modules;
2577
+ const FIND_MANY_USER = gql `
2578
+ query findManyUser($where: UserWhereInput!) {
2579
+ users(where: $where) {
2580
+ ${selectionSet}
2241
2581
  }
2242
- }
2243
- catch (error) {
2244
- if (error instanceof ApolloError && error.message === 'No User found') {
2245
- return null;
2582
+ }`;
2583
+ const variables = {
2584
+ where: whereInput ? whereInput : {
2585
+ id: props.id !== undefined ? props.id : undefined,
2586
+ name: props.name !== undefined ? props.name : undefined,
2587
+ email: props.email !== undefined ? props.email : undefined,
2588
+ emailVerified: props.emailVerified !== undefined ? {
2589
+ equals: props.emailVerified
2590
+ } : undefined,
2591
+ image: props.image !== undefined ? {
2592
+ equals: props.image
2593
+ } : undefined,
2594
+ createdAt: props.createdAt !== undefined ? {
2595
+ equals: props.createdAt
2596
+ } : undefined,
2597
+ updatedAt: props.updatedAt !== undefined ? {
2598
+ equals: props.updatedAt
2599
+ } : undefined,
2600
+ role: props.role !== undefined ? {
2601
+ equals: props.role
2602
+ } : undefined,
2603
+ bio: props.bio !== undefined ? {
2604
+ equals: props.bio
2605
+ } : undefined,
2606
+ jobTitle: props.jobTitle !== undefined ? {
2607
+ equals: props.jobTitle
2608
+ } : undefined,
2609
+ currentAccount: props.currentAccount !== undefined ? {
2610
+ equals: props.currentAccount
2611
+ } : undefined,
2612
+ plan: props.plan !== undefined ? {
2613
+ equals: props.plan
2614
+ } : undefined,
2615
+ openaiAPIKey: props.openaiAPIKey !== undefined ? {
2616
+ equals: props.openaiAPIKey
2617
+ } : undefined,
2618
+ openaiModel: props.openaiModel !== undefined ? {
2619
+ equals: props.openaiModel
2620
+ } : undefined,
2621
+ },
2622
+ };
2623
+ const filteredVariables = removeUndefinedProps(variables);
2624
+ const response = await client.query({
2625
+ query: FIND_MANY_USER,
2626
+ variables: filteredVariables,
2627
+ fetchPolicy: 'network-only', // Force network request to avoid stale cache
2628
+ });
2629
+ if (response.errors && response.errors.length > 0)
2630
+ throw new Error(response.errors[0].message);
2631
+ if (response && response.data && response.data.users) {
2632
+ return response.data.users;
2633
+ }
2634
+ else {
2635
+ return [];
2636
+ }
2246
2637
  }
2247
- else {
2248
- console.error('Error in getUser:', error);
2638
+ catch (error) {
2639
+ lastError = error;
2640
+ // Check if this is a "No record found" error - this is an expected condition, not a failure
2641
+ if (error.message === 'No User found') {
2642
+ return null;
2643
+ }
2644
+ // Check if this is a database connection error that we should retry
2645
+ const isConnectionError = error.message?.includes('Server has closed the connection') ||
2646
+ error.message?.includes('Cannot reach database server') ||
2647
+ error.message?.includes('Connection timed out') ||
2648
+ error.message?.includes('Accelerate') || // Prisma Accelerate proxy errors
2649
+ (error.networkError && error.networkError.message?.includes('Failed to fetch'));
2650
+ if (isConnectionError && retryCount < MAX_RETRIES - 1) {
2651
+ retryCount++;
2652
+ const delay = Math.pow(2, retryCount) * 100; // Exponential backoff: 200ms, 400ms, 800ms
2653
+ console.warn("Database connection error, retrying...");
2654
+ await new Promise(resolve => setTimeout(resolve, delay));
2655
+ continue;
2656
+ }
2657
+ // Log the error and rethrow
2658
+ console.error("Database error occurred:", error);
2249
2659
  throw error;
2250
2660
  }
2251
2661
  }
2662
+ // If we exhausted retries, throw the last error
2663
+ throw lastError;
2252
2664
  }
2253
2665
  };
2254
2666
  //# sourceMappingURL=User.js.map