@progressive-development/pd-provider-firebase-functions 0.3.1 → 0.3.2

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.
@@ -0,0 +1,51 @@
1
+ import { TrashItem } from './trash';
2
+ export interface AuthTrashOptions {
3
+ /**
4
+ * How to determine the Auth UID from the document.
5
+ * - 'documentId': Use the document ID as Auth UID (default)
6
+ * - string: Use this field name from the document data
7
+ */
8
+ authUidSource?: "documentId" | string;
9
+ /** Optional named database */
10
+ databaseId?: string;
11
+ }
12
+ /**
13
+ * Move document to trash AND disable the associated Auth user.
14
+ *
15
+ * Order of operations:
16
+ * 1. Disable Auth user (if this fails, nothing else happens)
17
+ * 2. Move document to trash
18
+ *
19
+ * @param collection - The collection path
20
+ * @param documentId - The document ID to trash
21
+ * @param deletedBy - UID of the user performing the deletion
22
+ * @param objectType - Type identifier for filtering
23
+ * @param options - Auth and database options
24
+ * @returns The created trash item
25
+ */
26
+ export declare function moveToTrashWithAuth<T = unknown>(collection: string, documentId: string, deletedBy: string, objectType: string, options?: AuthTrashOptions): Promise<TrashItem<T>>;
27
+ /**
28
+ * Restore document from trash AND re-enable the Auth user.
29
+ *
30
+ * Order of operations:
31
+ * 1. Get trash item to find Auth UID
32
+ * 2. Restore document from trash
33
+ * 3. Re-enable Auth user
34
+ *
35
+ * @param trashItemId - The trash document ID
36
+ * @param options - Auth and database options
37
+ */
38
+ export declare function restoreFromTrashWithAuth(trashItemId: string, options?: AuthTrashOptions): Promise<void>;
39
+ /**
40
+ * Permanently delete from trash AND delete the Auth user.
41
+ *
42
+ * Order of operations:
43
+ * 1. Get trash item to find Auth UID
44
+ * 2. Delete from trash
45
+ * 3. Delete Auth user
46
+ *
47
+ * @param trashItemId - The trash document ID
48
+ * @param options - Auth and database options
49
+ */
50
+ export declare function permanentDeleteWithAuth(trashItemId: string, options?: AuthTrashOptions): Promise<void>;
51
+ //# sourceMappingURL=auth-trash.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth-trash.d.ts","sourceRoot":"","sources":["../src/auth-trash.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAgE,KAAK,SAAS,EAAE,MAAM,SAAS,CAAC;AAQvG,MAAM,WAAW,gBAAgB;IAC/B;;;;OAIG;IACH,aAAa,CAAC,EAAE,YAAY,GAAG,MAAM,CAAC;IAEtC,8BAA8B;IAC9B,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAkCD;;;;;;;;;;;;;GAaG;AACH,wBAAsB,mBAAmB,CAAC,CAAC,GAAG,OAAO,EACnD,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,EAClB,SAAS,EAAE,MAAM,EACjB,UAAU,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,gBAAgB,GACzB,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,CA0BvB;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,wBAAwB,CAC5C,WAAW,EAAE,MAAM,EACnB,OAAO,CAAC,EAAE,gBAAgB,GACzB,OAAO,CAAC,IAAI,CAAC,CAsBf;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,uBAAuB,CAC3C,WAAW,EAAE,MAAM,EACnB,OAAO,CAAC,EAAE,gBAAgB,GACzB,OAAO,CAAC,IAAI,CAAC,CAqBf"}
@@ -0,0 +1,66 @@
1
+ import { getAuth } from 'firebase-admin/auth';
2
+ import { moveToTrash, getTrashItem, restoreFromTrash, permanentDelete } from './trash.js';
3
+ import { NotFoundError } from './errors.js';
4
+ import { logger } from './logger.js';
5
+
6
+ function getAuthUidFromTrashItem(trashItem, options) {
7
+ const source = options?.authUidSource ?? "documentId";
8
+ if (source === "documentId") {
9
+ return trashItem.originalId;
10
+ }
11
+ const data = trashItem.data;
12
+ const uid = data[source];
13
+ if (typeof uid !== "string" || !uid) {
14
+ throw new Error(`Auth UID field '${source}' not found or empty in trash item data`);
15
+ }
16
+ return uid;
17
+ }
18
+ async function moveToTrashWithAuth(collection, documentId, deletedBy, objectType, options) {
19
+ const authUid = options?.authUidSource === "documentId" || !options?.authUidSource ? documentId : documentId;
20
+ logger.info("Disabling Auth user before trash", { authUid, collection, documentId });
21
+ await getAuth().updateUser(authUid, { disabled: true });
22
+ logger.info("Auth user disabled", { authUid });
23
+ const trashItem = await moveToTrash(
24
+ collection,
25
+ documentId,
26
+ deletedBy,
27
+ objectType,
28
+ options?.databaseId
29
+ );
30
+ logger.info("Document moved to trash with Auth disabled", {
31
+ trashItemId: trashItem.id,
32
+ authUid
33
+ });
34
+ return trashItem;
35
+ }
36
+ async function restoreFromTrashWithAuth(trashItemId, options) {
37
+ const trashItem = await getTrashItem(trashItemId, options?.databaseId);
38
+ if (!trashItem) {
39
+ throw new NotFoundError(`Trash item ${trashItemId} not found`);
40
+ }
41
+ const authUid = getAuthUidFromTrashItem(trashItem, options);
42
+ logger.info("Restoring from trash with Auth re-enable", {
43
+ trashItemId,
44
+ authUid,
45
+ originalCollection: trashItem.originalCollection
46
+ });
47
+ await restoreFromTrash(trashItemId, options?.databaseId);
48
+ await getAuth().updateUser(authUid, { disabled: false });
49
+ logger.info("Document restored and Auth user re-enabled", { trashItemId, authUid });
50
+ }
51
+ async function permanentDeleteWithAuth(trashItemId, options) {
52
+ const trashItem = await getTrashItem(trashItemId, options?.databaseId);
53
+ if (!trashItem) {
54
+ throw new NotFoundError(`Trash item ${trashItemId} not found`);
55
+ }
56
+ const authUid = getAuthUidFromTrashItem(trashItem, options);
57
+ logger.info("Permanently deleting with Auth user deletion", {
58
+ trashItemId,
59
+ authUid
60
+ });
61
+ await permanentDelete(trashItemId, options?.databaseId);
62
+ await getAuth().deleteUser(authUid);
63
+ logger.info("Trash item and Auth user permanently deleted", { trashItemId, authUid });
64
+ }
65
+
66
+ export { moveToTrashWithAuth, permanentDeleteWithAuth, restoreFromTrashWithAuth };
package/dist/index.d.ts CHANGED
@@ -7,5 +7,7 @@ export { getDb } from './firestore';
7
7
  export { getNextCounter } from './counter';
8
8
  export type { TrashItem } from './trash';
9
9
  export { moveToTrash, restoreFromTrash, permanentDelete, listTrash, getTrashItem } from './trash';
10
+ export type { AuthTrashOptions } from './auth-trash';
11
+ export { moveToTrashWithAuth, restoreFromTrashWithAuth, permanentDeleteWithAuth } from './auth-trash';
10
12
  export { logger } from './logger';
11
13
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,YAAY,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGrC,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,iBAAiB,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAGvG,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AAGtE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAG3D,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAGpC,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAG3C,YAAY,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,eAAe,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAGlG,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,YAAY,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGrC,OAAO,EAAE,QAAQ,EAAE,eAAe,EAAE,iBAAiB,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAGvG,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AAGtE,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAG3D,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAGpC,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAG3C,YAAY,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,EAAE,WAAW,EAAE,gBAAgB,EAAE,eAAe,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAGlG,YAAY,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AACrD,OAAO,EAAE,mBAAmB,EAAE,wBAAwB,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAGtG,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC"}
package/dist/index.js CHANGED
@@ -5,4 +5,5 @@ export { created, handleError, success } from './response.js';
5
5
  export { getDb } from './firestore.js';
6
6
  export { getNextCounter } from './counter.js';
7
7
  export { getTrashItem, listTrash, moveToTrash, permanentDelete, restoreFromTrash } from './trash.js';
8
+ export { moveToTrashWithAuth, permanentDeleteWithAuth, restoreFromTrashWithAuth } from './auth-trash.js';
8
9
  export { logger } from './logger.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@progressive-development/pd-provider-firebase-functions",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Firebase Functions v2 utilities for pd-spa-helper backend",
5
5
  "author": "PD Progressive Development",
6
6
  "license": "SEE LICENSE IN LICENSE",
@@ -17,15 +17,6 @@
17
17
  "README.md",
18
18
  "LICENSE"
19
19
  ],
20
- "scripts": {
21
- "build": "vite build",
22
- "clean": "rm -rf dist",
23
- "clean:all": "rm -rf dist node_modules pnpm-lock.yaml",
24
- "lint": "eslint --ext .ts src --ignore-path ../../.eslintignore && prettier \"**/*.ts\" --check --ignore-path ../../.eslintignore",
25
- "format": "eslint --ext .ts src --fix --ignore-path ../../.eslintignore && prettier \"**/*.ts\" --write --ignore-path ../../.eslintignore",
26
- "check": "pnpm run lint && pnpm run build",
27
- "prepublishOnly": "pnpm run clean && pnpm run build"
28
- },
29
20
  "dependencies": {
30
21
  "firebase-admin": "^12.6.0",
31
22
  "firebase-functions": "^6.0.0"
@@ -41,5 +32,13 @@
41
32
  "firebase",
42
33
  "functions",
43
34
  "backend"
44
- ]
45
- }
35
+ ],
36
+ "scripts": {
37
+ "build": "vite build",
38
+ "clean": "rm -rf dist",
39
+ "clean:all": "rm -rf dist node_modules pnpm-lock.yaml",
40
+ "lint": "eslint --ext .ts src --ignore-path ../../.eslintignore && prettier \"**/*.ts\" --check --ignore-path ../../.eslintignore",
41
+ "format": "eslint --ext .ts src --fix --ignore-path ../../.eslintignore && prettier \"**/*.ts\" --write --ignore-path ../../.eslintignore",
42
+ "check": "pnpm run lint && pnpm run build"
43
+ }
44
+ }