@donotdev/functions 0.0.6 → 0.0.7

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 (56) hide show
  1. package/lib/firebase/index.js +8026 -32339
  2. package/lib/firebase/index.js.map +4 -4
  3. package/lib/shared/index.js +5973 -31507
  4. package/lib/shared/index.js.map +4 -4
  5. package/lib/vercel/api/index.js +4173 -30252
  6. package/lib/vercel/api/index.js.map +4 -4
  7. package/package.json +3 -3
  8. package/src/firebase/auth/getCustomClaims.ts +2 -1
  9. package/src/firebase/auth/getUserAuthStatus.ts +2 -1
  10. package/src/firebase/auth/removeCustomClaims.ts +2 -1
  11. package/src/firebase/auth/setCustomClaims.ts +3 -1
  12. package/src/firebase/baseFunction.ts +167 -65
  13. package/src/firebase/billing/cancelSubscription.ts +2 -1
  14. package/src/firebase/billing/changePlan.ts +1 -1
  15. package/src/firebase/billing/createCheckoutSession.ts +2 -2
  16. package/src/firebase/billing/createCustomerPortal.ts +2 -1
  17. package/src/firebase/billing/refreshSubscriptionStatus.ts +1 -1
  18. package/src/firebase/billing/webhookHandler.ts +3 -1
  19. package/src/firebase/config/constants.ts +12 -2
  20. package/src/firebase/crud/aggregate.ts +2 -2
  21. package/src/firebase/crud/create.ts +10 -12
  22. package/src/firebase/crud/delete.ts +9 -11
  23. package/src/firebase/crud/get.ts +21 -11
  24. package/src/firebase/crud/list.ts +80 -25
  25. package/src/firebase/crud/update.ts +9 -11
  26. package/src/firebase/helpers/githubAccessHelper.ts +2 -1
  27. package/src/firebase/helpers/githubTeamAccessHelper.ts +2 -1
  28. package/src/firebase/index.ts +7 -0
  29. package/src/firebase/oauth/disconnect.ts +1 -1
  30. package/src/firebase/oauth/exchangeToken.ts +4 -4
  31. package/src/firebase/oauth/getConnections.ts +1 -1
  32. package/src/firebase/oauth/githubAccess.ts +3 -2
  33. package/src/firebase/oauth/refreshToken.ts +4 -4
  34. package/src/firebase/registerCrudFunctions.ts +127 -0
  35. package/src/firebase/scheduled/checkExpiredSubscriptions.ts +3 -2
  36. package/src/shared/billing/webhookHandler.ts +1 -1
  37. package/src/shared/errorHandling.ts +1 -1
  38. package/src/shared/utils/external/subscription.ts +4 -5
  39. package/src/shared/utils/firebaseHelpers.ts +3 -1
  40. package/src/shared/utils/internal/idempotency.ts +2 -1
  41. package/src/shared/utils/internal/monitoring.ts +2 -1
  42. package/src/shared/utils/internal/rateLimiter.ts +2 -1
  43. package/src/shared/utils.ts +56 -7
  44. package/src/vercel/api/billing/cancel.ts +2 -1
  45. package/src/vercel/api/billing/change-plan.ts +1 -1
  46. package/src/vercel/api/billing/create-checkout-session.ts +2 -2
  47. package/src/vercel/api/billing/customer-portal.ts +2 -1
  48. package/src/vercel/api/billing/refresh-subscription-status.ts +1 -1
  49. package/src/vercel/api/crud/create.ts +2 -3
  50. package/src/vercel/api/crud/delete.ts +0 -1
  51. package/src/vercel/api/crud/get.ts +2 -3
  52. package/src/vercel/api/crud/list.ts +2 -3
  53. package/src/vercel/api/crud/update.ts +2 -3
  54. package/src/vercel/api/oauth/check-github-access.ts +1 -1
  55. package/src/vercel/api/oauth/grant-github-access.ts +1 -1
  56. package/src/vercel/api/oauth/revoke-github-access.ts +1 -1
@@ -9,9 +9,10 @@
9
9
  * @author AMBROISE PARK Consulting
10
10
  */
11
11
 
12
- import { getFirebaseAdminFirestore } from '@donotdev/firebase/server';
13
12
  import { logger } from 'firebase-functions/v2';
14
13
 
14
+ import { getFirebaseAdminFirestore } from '@donotdev/firebase/server';
15
+
15
16
  /**
16
17
  * Payment metrics data structure
17
18
  *
@@ -9,9 +9,10 @@
9
9
  * @author AMBROISE PARK Consulting
10
10
  */
11
11
 
12
- import { getFirebaseAdminFirestore } from '@donotdev/firebase/server';
13
12
  import { logger } from 'firebase-functions/v2';
14
13
 
14
+ import { getFirebaseAdminFirestore } from '@donotdev/firebase/server';
15
+
15
16
  interface RateLimitConfig {
16
17
  maxAttempts: number;
17
18
  windowMs: number;
@@ -12,6 +12,7 @@
12
12
  import Stripe from 'stripe';
13
13
 
14
14
  import { DoNotDevError } from '@donotdev/core/server';
15
+ import type { OAuthPartnerId, UserRole } from '@donotdev/core/server';
15
16
  import {
16
17
  getFirebaseAdminAuth,
17
18
  getFirebaseAdminFirestore,
@@ -20,7 +21,6 @@ import {
20
21
 
21
22
  // Re-export DoNotDevError for external use
22
23
  export { DoNotDevError };
23
- import type { OAuthPartnerId } from '@donotdev/core/server';
24
24
 
25
25
  /**
26
26
  * Get Firestore instance using @donotdev/firebase/server
@@ -114,6 +114,37 @@ export function assertAuthenticated(auth: any): string {
114
114
  return auth.uid;
115
115
  }
116
116
 
117
+ /**
118
+ * Get user role from Firebase auth context
119
+ *
120
+ * Determines the user's role for visibility filtering based on:
121
+ * - auth.uid: If missing → 'guest'
122
+ * - auth.token.isSuper: If true → 'super'
123
+ * - auth.token.isAdmin: If true → 'admin'
124
+ * - Otherwise → 'user' (authenticated)
125
+ *
126
+ * @param auth - Firebase auth context from callable function
127
+ * @returns UserRole for visibility filtering
128
+ *
129
+ * @version 0.0.1
130
+ * @since 0.0.1
131
+ * @author AMBROISE PARK Consulting
132
+ */
133
+ export function getUserRole(auth: any): UserRole {
134
+ if (!auth?.uid) return 'guest';
135
+
136
+ // Check role claim (standard pattern: auth.token.role = 'admin' | 'super' | etc.)
137
+ const role = auth.token?.role;
138
+ if (role === 'super') return 'super';
139
+ if (role === 'admin') return 'admin';
140
+
141
+ // Fallback: check legacy boolean flags for backward compatibility
142
+ if (auth.token?.isSuper) return 'super';
143
+ if (auth.token?.isAdmin) return 'admin';
144
+
145
+ return 'user';
146
+ }
147
+
117
148
  /**
118
149
  * Validates that required Stripe environment variables are set
119
150
  *
@@ -256,19 +287,37 @@ export async function handleSubscriptionCancellation(
256
287
 
257
288
  /**
258
289
  * Asserts that a user has admin privileges
290
+ * Uses role hierarchy: super > admin > user > guest
259
291
  *
260
- * @version 0.0.1
292
+ * @version 0.0.2
261
293
  * @since 0.0.1
262
294
  * @author AMBROISE PARK Consulting
263
295
  */
264
- export function assertAdmin(uid: string): string {
296
+ export async function assertAdmin(uid: string): Promise<string> {
265
297
  if (!uid) {
266
- throw new Error('Authentication required');
298
+ throw new DoNotDevError('Authentication required', 'unauthenticated');
267
299
  }
268
300
 
269
- // TODO: Implement admin check logic
270
- // For now, just return the uid
271
- return uid;
301
+ try {
302
+ const user = await getFirebaseAdminAuth().getUser(uid);
303
+ const claims = user.customClaims || {};
304
+
305
+ // Check role claim first (standard pattern)
306
+ const role = claims.role;
307
+ if (role === 'admin' || role === 'super') {
308
+ return uid;
309
+ }
310
+
311
+ // Fallback: check legacy boolean flags
312
+ if (claims.isAdmin === true || claims.isSuper === true) {
313
+ return uid;
314
+ }
315
+
316
+ throw new DoNotDevError('Admin privileges required', 'permission-denied');
317
+ } catch (error) {
318
+ if (error instanceof DoNotDevError) throw error;
319
+ throw new DoNotDevError('Failed to verify admin status', 'internal');
320
+ }
272
321
  }
273
322
 
274
323
  /**
@@ -9,9 +9,10 @@
9
9
  * @author AMBROISE PARK Consulting
10
10
  */
11
11
 
12
- import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
13
12
  import * as v from 'valibot';
14
13
 
14
+ import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
15
+
15
16
  import { cancelUserSubscription } from '../../../shared/billing/helpers/subscriptionManagement.js';
16
17
  import { createVercelBaseFunction } from '../../baseFunction.js';
17
18
 
@@ -9,11 +9,11 @@
9
9
  * @author AMBROISE PARK Consulting
10
10
  */
11
11
 
12
- import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
13
12
  import Stripe from 'stripe';
14
13
  import * as v from 'valibot';
15
14
 
16
15
  import type { StripeBackConfig } from '@donotdev/core/server';
16
+ import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
17
17
 
18
18
  import { updateUserSubscription } from '../../../shared/billing/helpers/updateUserSubscription.js';
19
19
  import { handleError } from '../../../shared/errorHandling.js';
@@ -9,14 +9,14 @@
9
9
  * @author AMBROISE PARK Consulting
10
10
  */
11
11
 
12
- import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
13
12
  import * as v from 'valibot';
14
13
 
14
+ import { CreateCheckoutSessionRequestSchema } from '@donotdev/core/server';
15
15
  import type {
16
16
  CreateCheckoutSessionRequest,
17
17
  StripeBackConfig,
18
18
  } from '@donotdev/core/server';
19
- import { CreateCheckoutSessionRequestSchema } from '@donotdev/core/server';
19
+ import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
20
20
 
21
21
  import { createCheckoutAlgorithm } from '../../../shared/billing/createCheckout.js';
22
22
  import { stripe, validateStripeEnvironment } from '../../../shared/utils.js';
@@ -9,9 +9,10 @@
9
9
  * @author AMBROISE PARK Consulting
10
10
  */
11
11
 
12
- import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
13
12
  import * as v from 'valibot';
14
13
 
14
+ import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
15
+
15
16
  import { handleError } from '../../../shared/errorHandling.js';
16
17
  import { stripe, validateStripeEnvironment } from '../../../shared/utils.js';
17
18
  import { createVercelBaseFunction } from '../../baseFunction.js';
@@ -9,11 +9,11 @@
9
9
  * @author AMBROISE PARK Consulting
10
10
  */
11
11
 
12
- import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
13
12
  import Stripe from 'stripe';
14
13
  import * as v from 'valibot';
15
14
 
16
15
  import type { RefreshSubscriptionRequest } from '@donotdev/core/server';
16
+ import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
17
17
 
18
18
  import { stripe, validateStripeEnvironment } from '../../../shared/utils.js';
19
19
  import { createVercelBaseFunction } from '../../baseFunction.js';
@@ -9,12 +9,11 @@
9
9
  * @author AMBROISE PARK Consulting
10
10
  */
11
11
 
12
- import type { CreateEntityData } from '@donotdev/core/server';
13
12
  import { DEFAULT_STATUS_VALUE } from '@donotdev/core/server';
14
-
15
- import { handleError } from '../../../shared/errorHandling.js';
13
+ import type { CreateEntityData } from '@donotdev/core/server';
16
14
  import { getFirebaseAdminFirestore } from '@donotdev/firebase/server';
17
15
 
16
+ import { handleError } from '../../../shared/errorHandling.js';
18
17
  import {
19
18
  prepareForFirestore,
20
19
  transformFirestoreData,
@@ -10,7 +10,6 @@
10
10
  */
11
11
 
12
12
  import type { GetEntityData } from '@donotdev/core/server';
13
-
14
13
  import { getFirebaseAdminFirestore } from '@donotdev/firebase/server';
15
14
 
16
15
  import { handleError } from '../../../shared/errorHandling.js';
@@ -9,14 +9,13 @@
9
9
  * @author AMBROISE PARK Consulting
10
10
  */
11
11
 
12
- import type { GetEntityData } from '@donotdev/core/server';
13
12
  import { HIDDEN_STATUSES } from '@donotdev/core/server';
13
+ import type { GetEntityData } from '@donotdev/core/server';
14
+ import { getFirebaseAdminFirestore } from '@donotdev/firebase/server';
14
15
 
15
16
  import { handleError } from '../../../shared/errorHandling.js';
16
17
  import { transformFirestoreData } from '../../../shared/index.js';
17
18
  import { filterVisibleFields } from '../../../shared/index.js';
18
- import { getFirebaseAdminFirestore } from '@donotdev/firebase/server';
19
-
20
19
  import { assertAuthenticated } from '../../../shared/utils.js';
21
20
 
22
21
  import type { NextApiRequest, NextApiResponse } from 'next';
@@ -9,14 +9,13 @@
9
9
  * @author AMBROISE PARK Consulting
10
10
  */
11
11
 
12
- import type { ListEntityData } from '@donotdev/core/server';
13
12
  import { HIDDEN_STATUSES } from '@donotdev/core/server';
13
+ import type { ListEntityData } from '@donotdev/core/server';
14
+ import { getFirebaseAdminFirestore } from '@donotdev/firebase/server';
14
15
 
15
16
  import { handleError } from '../../../shared/errorHandling.js';
16
17
  import { transformFirestoreData } from '../../../shared/index.js';
17
18
  import { filterVisibleFields } from '../../../shared/index.js';
18
- import { getFirebaseAdminFirestore } from '@donotdev/firebase/server';
19
-
20
19
  import { assertAuthenticated } from '../../../shared/utils.js';
21
20
 
22
21
  import type { NextApiRequest, NextApiResponse } from 'next';
@@ -9,12 +9,11 @@
9
9
  * @author AMBROISE PARK Consulting
10
10
  */
11
11
 
12
- import type { UpdateEntityData } from '@donotdev/core/server';
13
12
  import { DEFAULT_STATUS_VALUE } from '@donotdev/core/server';
14
-
15
- import { handleError } from '../../../shared/errorHandling.js';
13
+ import type { UpdateEntityData } from '@donotdev/core/server';
16
14
  import { getFirebaseAdminFirestore } from '@donotdev/firebase/server';
17
15
 
16
+ import { handleError } from '../../../shared/errorHandling.js';
18
17
  import {
19
18
  prepareForFirestore,
20
19
  transformFirestoreData,
@@ -9,13 +9,13 @@
9
9
  * @author AMBROISE PARK Consulting
10
10
  */
11
11
 
12
- import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
13
12
  import * as v from 'valibot';
14
13
 
15
14
  import {
16
15
  checkGitHubAccessSchema,
17
16
  type CheckGitHubAccessRequest,
18
17
  } from '@donotdev/core/server';
18
+ import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
19
19
 
20
20
  import { GitHubApiService } from '../../../shared/index.js';
21
21
  import { createVercelBaseFunction } from '../../baseFunction.js';
@@ -9,13 +9,13 @@
9
9
  * @author AMBROISE PARK Consulting
10
10
  */
11
11
 
12
- import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
13
12
  import * as v from 'valibot';
14
13
 
15
14
  import {
16
15
  grantGitHubAccessSchema,
17
16
  type GrantGitHubAccessRequest,
18
17
  } from '@donotdev/core/server';
18
+ import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
19
19
 
20
20
  import { GitHubApiService } from '../../../shared/index.js';
21
21
  import { createVercelBaseFunction } from '../../baseFunction.js';
@@ -9,13 +9,13 @@
9
9
  * @author AMBROISE PARK Consulting
10
10
  */
11
11
 
12
- import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
13
12
  import * as v from 'valibot';
14
13
 
15
14
  import {
16
15
  revokeGitHubAccessSchema,
17
16
  type RevokeGitHubAccessRequest,
18
17
  } from '@donotdev/core/server';
18
+ import { getFirebaseAdminAuth } from '@donotdev/firebase/server';
19
19
 
20
20
  import { handleError } from '../../../shared/errorHandling.js';
21
21
  import { GitHubApiService } from '../../../shared/index.js';