@progressive-development/pd-provider-firebase-functions 0.3.1 → 0.3.3
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/dist/auth-trash.d.ts +51 -0
- package/dist/auth-trash.d.ts.map +1 -0
- package/dist/auth-trash.js +96 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/package.json +11 -12
|
@@ -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 user doesn't exist, continue anyway)
|
|
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 (if user doesn't exist, log warning)
|
|
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 (ignore if user doesn't exist)
|
|
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,CAmCvB;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,wBAAwB,CAC5C,WAAW,EAAE,MAAM,EACnB,OAAO,CAAC,EAAE,gBAAgB,GACzB,OAAO,CAAC,IAAI,CAAC,CAiCf;AAED;;;;;;;;;;GAUG;AACH,wBAAsB,uBAAuB,CAC3C,WAAW,EAAE,MAAM,EACnB,OAAO,CAAC,EAAE,gBAAgB,GACzB,OAAO,CAAC,IAAI,CAAC,CA6Bf"}
|
|
@@ -0,0 +1,96 @@
|
|
|
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
|
+
try {
|
|
22
|
+
await getAuth().updateUser(authUid, { disabled: true });
|
|
23
|
+
logger.info("Auth user disabled", { authUid });
|
|
24
|
+
} catch (error) {
|
|
25
|
+
const authError = error;
|
|
26
|
+
if (authError.code === "auth/user-not-found") {
|
|
27
|
+
logger.info("Auth user not found, continuing with trash operation", { authUid });
|
|
28
|
+
} else {
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const trashItem = await moveToTrash(
|
|
33
|
+
collection,
|
|
34
|
+
documentId,
|
|
35
|
+
deletedBy,
|
|
36
|
+
objectType,
|
|
37
|
+
options?.databaseId
|
|
38
|
+
);
|
|
39
|
+
logger.info("Document moved to trash with Auth disabled", {
|
|
40
|
+
trashItemId: trashItem.id,
|
|
41
|
+
authUid
|
|
42
|
+
});
|
|
43
|
+
return trashItem;
|
|
44
|
+
}
|
|
45
|
+
async function restoreFromTrashWithAuth(trashItemId, options) {
|
|
46
|
+
const trashItem = await getTrashItem(trashItemId, options?.databaseId);
|
|
47
|
+
if (!trashItem) {
|
|
48
|
+
throw new NotFoundError(`Trash item ${trashItemId} not found`);
|
|
49
|
+
}
|
|
50
|
+
const authUid = getAuthUidFromTrashItem(trashItem, options);
|
|
51
|
+
logger.info("Restoring from trash with Auth re-enable", {
|
|
52
|
+
trashItemId,
|
|
53
|
+
authUid,
|
|
54
|
+
originalCollection: trashItem.originalCollection
|
|
55
|
+
});
|
|
56
|
+
await restoreFromTrash(trashItemId, options?.databaseId);
|
|
57
|
+
try {
|
|
58
|
+
await getAuth().updateUser(authUid, { disabled: false });
|
|
59
|
+
logger.info("Document restored and Auth user re-enabled", { trashItemId, authUid });
|
|
60
|
+
} catch (error) {
|
|
61
|
+
const authError = error;
|
|
62
|
+
if (authError.code === "auth/user-not-found") {
|
|
63
|
+
logger.warn("Document restored but Auth user not found - manual Auth user creation required", {
|
|
64
|
+
trashItemId,
|
|
65
|
+
authUid
|
|
66
|
+
});
|
|
67
|
+
} else {
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
async function permanentDeleteWithAuth(trashItemId, options) {
|
|
73
|
+
const trashItem = await getTrashItem(trashItemId, options?.databaseId);
|
|
74
|
+
if (!trashItem) {
|
|
75
|
+
throw new NotFoundError(`Trash item ${trashItemId} not found`);
|
|
76
|
+
}
|
|
77
|
+
const authUid = getAuthUidFromTrashItem(trashItem, options);
|
|
78
|
+
logger.info("Permanently deleting with Auth user deletion", {
|
|
79
|
+
trashItemId,
|
|
80
|
+
authUid
|
|
81
|
+
});
|
|
82
|
+
await permanentDelete(trashItemId, options?.databaseId);
|
|
83
|
+
try {
|
|
84
|
+
await getAuth().deleteUser(authUid);
|
|
85
|
+
logger.info("Trash item and Auth user permanently deleted", { trashItemId, authUid });
|
|
86
|
+
} catch (error) {
|
|
87
|
+
const authError = error;
|
|
88
|
+
if (authError.code === "auth/user-not-found") {
|
|
89
|
+
logger.info("Auth user not found, trash item deleted", { trashItemId, authUid });
|
|
90
|
+
} else {
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
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
|
package/dist/index.d.ts.map
CHANGED
|
@@ -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,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 @@ 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.
|
|
3
|
+
"version": "0.3.3",
|
|
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
|
+
}
|