@edgedev/firebase 2.1.66 → 2.1.68
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/edgeFirebase.ts +78 -15
- package/package.json +1 -1
package/edgeFirebase.ts
CHANGED
|
@@ -125,6 +125,7 @@ interface newUser {
|
|
|
125
125
|
specialPermissions: specialPermission[];
|
|
126
126
|
meta: object;
|
|
127
127
|
isTemplate?: boolean;
|
|
128
|
+
customRegCode?: string;
|
|
128
129
|
subCreate?: {
|
|
129
130
|
rootPath: string, // This must be a collection path (odd number of segments) since a document will be created and assigned to ther user here.
|
|
130
131
|
role: string,
|
|
@@ -728,27 +729,59 @@ export const EdgeFirebase = class {
|
|
|
728
729
|
});
|
|
729
730
|
}
|
|
730
731
|
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
732
|
+
// Build Firestore dotted-path updates so meta merges instead of replacing
|
|
733
|
+
const incomingMeta =
|
|
734
|
+
(Object.prototype.hasOwnProperty.call(userRegister, 'meta') &&
|
|
735
|
+
userRegister.meta && typeof userRegister.meta === 'object')
|
|
736
|
+
? (userRegister.meta as any)
|
|
737
|
+
: {};
|
|
738
|
+
|
|
739
|
+
// Identity fields written at top level
|
|
740
|
+
let stagedUserUpdate: {
|
|
741
|
+
userId?: string;
|
|
742
|
+
templateUserId?: string;
|
|
743
|
+
dynamicDocumentFieldValue?: string;
|
|
744
|
+
uid: string;
|
|
745
|
+
requestedOrgId?: string;
|
|
746
|
+
} = {
|
|
747
|
+
userId: response.user.uid,
|
|
748
|
+
uid: response.user.uid
|
|
749
|
+
};
|
|
750
|
+
|
|
751
|
+
let dottedUpdates: Record<string, any> = {};
|
|
752
|
+
|
|
738
753
|
if (user.isTemplate) {
|
|
739
|
-
|
|
754
|
+
// Template flow: keep template `meta` intact; write user fields as templateMeta.*
|
|
755
|
+
stagedUserUpdate = { templateUserId: response.user.uid, uid: response.user.uid };
|
|
756
|
+
|
|
740
757
|
if (Object.prototype.hasOwnProperty.call(userRegister, 'dynamicDocumentFieldValue')) {
|
|
741
|
-
stagedUserUpdate =
|
|
758
|
+
stagedUserUpdate.dynamicDocumentFieldValue = userRegister.dynamicDocumentFieldValue;
|
|
759
|
+
}
|
|
760
|
+
if (Object.prototype.hasOwnProperty.call(userRegister, 'requestedOrgId')) {
|
|
761
|
+
stagedUserUpdate.requestedOrgId = userRegister.requestedOrgId?.toLowerCase();
|
|
742
762
|
}
|
|
763
|
+
|
|
764
|
+
// Only write templateMeta.* dotted paths (do not touch meta)
|
|
765
|
+
dottedUpdates = { ...this.flattenObjectPaths(incomingMeta, 'templateMeta') };
|
|
766
|
+
} else {
|
|
767
|
+
// Non-template: merge user meta into existing meta using dotted paths
|
|
743
768
|
if (Object.prototype.hasOwnProperty.call(userRegister, 'requestedOrgId')) {
|
|
744
|
-
stagedUserUpdate =
|
|
769
|
+
stagedUserUpdate.requestedOrgId = userRegister.requestedOrgId?.toLowerCase();
|
|
745
770
|
}
|
|
771
|
+
dottedUpdates = { ...this.flattenObjectPaths(incomingMeta, 'meta') };
|
|
746
772
|
}
|
|
747
|
-
|
|
748
|
-
initRoleHelper
|
|
773
|
+
|
|
774
|
+
const initRoleHelper: any = { uid: response.user.uid };
|
|
775
|
+
initRoleHelper["edge-assignment-helper"] = { permissionType: "roles" };
|
|
749
776
|
this.user.loggingIn = true;
|
|
750
777
|
await setDoc(doc(this.db, "rule-helpers", response.user.uid), initRoleHelper);
|
|
751
|
-
|
|
778
|
+
|
|
779
|
+
// Firestore merges only the specified dotted fields inside meta/templateMeta.
|
|
780
|
+
await updateDoc(
|
|
781
|
+
doc(this.db, "staged-users/" + userRegister.registrationCode),
|
|
782
|
+
{ ...stagedUserUpdate, ...dottedUpdates }
|
|
783
|
+
);
|
|
784
|
+
|
|
752
785
|
this.logAnalyticsEvent("sign_up", { uid: response.user.uid});
|
|
753
786
|
return this.sendResponse({
|
|
754
787
|
success: true,
|
|
@@ -1023,6 +1056,29 @@ export const EdgeFirebase = class {
|
|
|
1023
1056
|
return response;
|
|
1024
1057
|
};
|
|
1025
1058
|
|
|
1059
|
+
private flattenObjectPaths = (obj: any, base: string): Record<string, any> => {
|
|
1060
|
+
const out: Record<string, any> = {};
|
|
1061
|
+
const walk = (cur: any, prefix: string) => {
|
|
1062
|
+
if (cur === null || cur === undefined) {
|
|
1063
|
+
out[prefix] = cur;
|
|
1064
|
+
return;
|
|
1065
|
+
}
|
|
1066
|
+
if (typeof cur !== 'object' || Array.isArray(cur)) {
|
|
1067
|
+
out[prefix] = cur;
|
|
1068
|
+
return;
|
|
1069
|
+
}
|
|
1070
|
+
for (const [k, v] of Object.entries(cur)) {
|
|
1071
|
+
walk(v, `${prefix}.${k}`);
|
|
1072
|
+
}
|
|
1073
|
+
};
|
|
1074
|
+
if (obj && typeof obj === 'object') {
|
|
1075
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
1076
|
+
walk(v, `${base}.${k}`);
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
return out;
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1026
1082
|
private setRuleHelper = async(collectionPath: string, action): Promise<void> => {
|
|
1027
1083
|
const collection = collectionPath.replaceAll("-", "/").split("/");
|
|
1028
1084
|
let ruleKey = collectionPath.replaceAll("/", "-");
|
|
@@ -1150,8 +1206,10 @@ export const EdgeFirebase = class {
|
|
|
1150
1206
|
private generateUserMeta = async (userMeta: newUser): Promise<actionResponse> => {
|
|
1151
1207
|
const roles: role[] = userMeta.roles || [];
|
|
1152
1208
|
const specialPermissions: specialPermission[] = userMeta.specialPermissions || [];
|
|
1209
|
+
const docId = userMeta?.customRegCode || "";
|
|
1153
1210
|
delete userMeta.roles;
|
|
1154
1211
|
delete userMeta.specialPermissions;
|
|
1212
|
+
delete userMeta.customRegCode;
|
|
1155
1213
|
|
|
1156
1214
|
let isTemplate = false
|
|
1157
1215
|
if (Object.prototype.hasOwnProperty.call(userMeta, "isTemplate") && userMeta.isTemplate) {
|
|
@@ -1180,8 +1238,13 @@ export const EdgeFirebase = class {
|
|
|
1180
1238
|
}
|
|
1181
1239
|
|
|
1182
1240
|
const onlyMeta = { meta: userMeta.meta, userId: "", uid: this.user.uid, roles:{}, specialPermissions:{}, isTemplate, subCreate, templateUserId: "" };
|
|
1183
|
-
|
|
1184
|
-
|
|
1241
|
+
let docRef;
|
|
1242
|
+
if (!docId) {
|
|
1243
|
+
docRef = await addDoc(collection(this.db, "staged-users"), onlyMeta );
|
|
1244
|
+
} else {
|
|
1245
|
+
docRef = doc(this.db, "staged-users", docId);
|
|
1246
|
+
await setDoc(docRef, onlyMeta);
|
|
1247
|
+
}
|
|
1185
1248
|
for (const role of roles) {
|
|
1186
1249
|
await this.storeUserRoles(docRef.id, role.collectionPath, role.role);
|
|
1187
1250
|
}
|