@donotdev/functions 0.0.6 → 0.0.8
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.
- package/lib/firebase/index.js +8026 -32339
- package/lib/firebase/index.js.map +4 -4
- package/lib/shared/index.js +5973 -31507
- package/lib/shared/index.js.map +4 -4
- package/lib/vercel/api/index.js +4173 -30252
- package/lib/vercel/api/index.js.map +4 -4
- package/package.json +3 -3
- package/src/firebase/auth/getCustomClaims.ts +2 -1
- package/src/firebase/auth/getUserAuthStatus.ts +2 -1
- package/src/firebase/auth/removeCustomClaims.ts +2 -1
- package/src/firebase/auth/setCustomClaims.ts +3 -1
- package/src/firebase/baseFunction.ts +167 -65
- package/src/firebase/billing/cancelSubscription.ts +2 -1
- package/src/firebase/billing/changePlan.ts +1 -1
- package/src/firebase/billing/createCheckoutSession.ts +2 -2
- package/src/firebase/billing/createCustomerPortal.ts +2 -1
- package/src/firebase/billing/refreshSubscriptionStatus.ts +1 -1
- package/src/firebase/billing/webhookHandler.ts +3 -1
- package/src/firebase/config/constants.ts +12 -2
- package/src/firebase/crud/aggregate.ts +2 -2
- package/src/firebase/crud/create.ts +10 -12
- package/src/firebase/crud/delete.ts +9 -11
- package/src/firebase/crud/get.ts +21 -11
- package/src/firebase/crud/list.ts +86 -29
- package/src/firebase/crud/update.ts +9 -11
- package/src/firebase/helpers/githubAccessHelper.ts +2 -1
- package/src/firebase/helpers/githubTeamAccessHelper.ts +2 -1
- package/src/firebase/index.ts +7 -0
- package/src/firebase/oauth/disconnect.ts +1 -1
- package/src/firebase/oauth/exchangeToken.ts +4 -4
- package/src/firebase/oauth/getConnections.ts +1 -1
- package/src/firebase/oauth/githubAccess.ts +3 -2
- package/src/firebase/oauth/refreshToken.ts +4 -4
- package/src/firebase/registerCrudFunctions.ts +127 -0
- package/src/firebase/scheduled/checkExpiredSubscriptions.ts +3 -2
- package/src/shared/billing/webhookHandler.ts +1 -1
- package/src/shared/errorHandling.ts +1 -1
- package/src/shared/utils/external/subscription.ts +19 -6
- package/src/shared/utils/firebaseHelpers.ts +3 -1
- package/src/shared/utils/internal/idempotency.ts +2 -1
- package/src/shared/utils/internal/monitoring.ts +2 -1
- package/src/shared/utils/internal/rateLimiter.ts +2 -1
- package/src/shared/utils.ts +56 -7
- package/src/vercel/api/billing/cancel.ts +2 -1
- package/src/vercel/api/billing/change-plan.ts +1 -1
- package/src/vercel/api/billing/create-checkout-session.ts +2 -2
- package/src/vercel/api/billing/customer-portal.ts +2 -1
- package/src/vercel/api/billing/refresh-subscription-status.ts +1 -1
- package/src/vercel/api/crud/create.ts +2 -3
- package/src/vercel/api/crud/delete.ts +0 -1
- package/src/vercel/api/crud/get.ts +2 -3
- package/src/vercel/api/crud/list.ts +2 -3
- package/src/vercel/api/crud/update.ts +2 -3
- package/src/vercel/api/oauth/check-github-access.ts +1 -1
- package/src/vercel/api/oauth/grant-github-access.ts +1 -1
- package/src/vercel/api/oauth/revoke-github-access.ts +1 -1
|
@@ -9,15 +9,15 @@
|
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import {
|
|
13
|
-
getFirebaseAdminAuth,
|
|
14
|
-
getFirebaseAdminFirestore,
|
|
15
|
-
} from '@donotdev/firebase/server';
|
|
16
|
-
|
|
17
12
|
import type {
|
|
18
13
|
SubscriptionStatus,
|
|
19
14
|
SubscriptionClaims,
|
|
20
15
|
} from '@donotdev/core/server';
|
|
16
|
+
import { SUBSCRIPTION_STATUS } from '@donotdev/core/server';
|
|
17
|
+
import {
|
|
18
|
+
getFirebaseAdminAuth,
|
|
19
|
+
getFirebaseAdminFirestore,
|
|
20
|
+
} from '@donotdev/firebase/server';
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
23
|
* Maps Stripe price ID to subscription tier
|
|
@@ -60,11 +60,24 @@ export async function updateUserSubscription(
|
|
|
60
60
|
const priceId = subscription.items?.data?.[0]?.price?.id;
|
|
61
61
|
const tier = getTierFromPriceId(priceId);
|
|
62
62
|
|
|
63
|
+
// Validate status: must be a framework value OR a valid custom consumer value (non-empty string)
|
|
64
|
+
// Default to 'incomplete' if invalid/empty
|
|
65
|
+
const frameworkStatuses = Object.values(SUBSCRIPTION_STATUS);
|
|
66
|
+
const rawStatus = subscription.status || '';
|
|
67
|
+
const isValidFrameworkStatus = frameworkStatuses.includes(rawStatus as any);
|
|
68
|
+
const isValidCustomStatus =
|
|
69
|
+
typeof rawStatus === 'string' && rawStatus.trim().length > 0;
|
|
70
|
+
|
|
71
|
+
const status: SubscriptionStatus =
|
|
72
|
+
isValidFrameworkStatus || isValidCustomStatus
|
|
73
|
+
? (rawStatus as SubscriptionStatus)
|
|
74
|
+
: SUBSCRIPTION_STATUS.INCOMPLETE;
|
|
75
|
+
|
|
63
76
|
const subscriptionClaims: SubscriptionClaims = {
|
|
64
77
|
tier,
|
|
65
78
|
subscriptionId: subscription.id,
|
|
66
79
|
customerId: subscription.customer,
|
|
67
|
-
status
|
|
80
|
+
status,
|
|
68
81
|
subscriptionEnd: subscription.current_period_end
|
|
69
82
|
? new Date(subscription.current_period_end * 1000).toISOString()
|
|
70
83
|
: null,
|
|
@@ -9,11 +9,13 @@
|
|
|
9
9
|
* @author AMBROISE PARK Consulting
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
-
import { onCall
|
|
12
|
+
import { onCall } from 'firebase-functions/v2/https';
|
|
13
13
|
import * as v from 'valibot';
|
|
14
14
|
|
|
15
15
|
import { withSchemaValidation } from './schemaValidation.js';
|
|
16
16
|
|
|
17
|
+
import type { CallableRequest } from 'firebase-functions/v2/https';
|
|
18
|
+
|
|
17
19
|
/**
|
|
18
20
|
* Creates a Firebase onCall function with schema validation
|
|
19
21
|
*
|
|
@@ -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
|
export interface IdempotencyResult<T = any> {
|
|
16
17
|
result: T;
|
|
17
18
|
processedAt: string;
|
|
@@ -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;
|
package/src/shared/utils.ts
CHANGED
|
@@ -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.
|
|
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
|
|
298
|
+
throw new DoNotDevError('Authentication required', 'unauthenticated');
|
|
267
299
|
}
|
|
268
300
|
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
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 {
|
|
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,
|
|
@@ -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';
|