Skip to content

Commit cbf4dbc

Browse files
committed
group-evaults
1 parent bb1de52 commit cbf4dbc

File tree

7 files changed

+141
-7
lines changed

7 files changed

+141
-7
lines changed

infrastructure/web3-adapter/src/mapper/mapper.ts

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,68 @@ export function getValueByPath(obj: Record<string, any>, path: string): any {
3333
}, obj);
3434
}
3535

36+
/**
37+
* Extracts the owner eVault from data using the specified path(s).
38+
* Supports fallback paths using the || operator.
39+
*
40+
* @param data - The data object to extract from
41+
* @param ownerEnamePath - The path(s) to extract from. Can be:
42+
* - Single path: "owner.ename"
43+
* - Fallback paths: "path1||path2" (tries path1 first, then path2)
44+
* - Table references: "users(owner.ename)"
45+
* - Fallback with table refs: "users(owner.ename)||users(creator.ename)"
46+
* @returns The owner eVault identifier or null if not found
47+
*/
3648
async function extractOwnerEvault(
3749
data: Record<string, unknown>,
3850
ownerEnamePath: string,
3951
): Promise<string | null> {
4052
if (!ownerEnamePath || ownerEnamePath === "null") {
4153
return null;
4254
}
55+
56+
// Check if the path contains fallback operator (||)
57+
if (ownerEnamePath.includes("||")) {
58+
const paths = ownerEnamePath.split("||").map(path => path.trim()).filter(path => path.length > 0);
59+
60+
if (paths.length < 2) {
61+
console.warn("Invalid fallback path format. Expected 'path1||path2' but got:", ownerEnamePath);
62+
return null;
63+
}
64+
65+
console.log(`Processing fallback paths for owner eVault: [${paths.join(", ")}]`);
66+
67+
// Try each path in order until one succeeds
68+
for (let i = 0; i < paths.length; i++) {
69+
const path = paths[i];
70+
console.log(`Trying fallback path ${i + 1}/${paths.length}: ${path}`);
71+
72+
const result = await extractOwnerEvaultSinglePath(data, path);
73+
if (result !== null) {
74+
console.log(`✅ Owner eVault found using fallback path ${i + 1}: ${path}`);
75+
return result;
76+
} else {
77+
console.log(`❌ Fallback path ${i + 1} failed: ${path}`);
78+
}
79+
}
80+
81+
// If all paths fail, return null
82+
console.log("❌ All fallback paths failed for owner eVault");
83+
return null;
84+
}
85+
86+
// Single path - use existing logic
87+
return await extractOwnerEvaultSinglePath(data, ownerEnamePath);
88+
}
89+
90+
/**
91+
* Helper function to extract owner eVault from a single path.
92+
* This is the original implementation logic for single paths.
93+
*/
94+
async function extractOwnerEvaultSinglePath(
95+
data: Record<string, unknown>,
96+
ownerEnamePath: string,
97+
): Promise<string | null> {
4398
if (!ownerEnamePath.includes("(")) {
4499
return (data[ownerEnamePath] as string) || null;
45100
}
@@ -49,9 +104,12 @@ async function extractOwnerEvault(
49104
let value = getValueByPath(data, fieldPath);
50105
if (Array.isArray(value)) return value[0];
51106
console.log("OWNER PATH", value);
52-
if (value.includes("(") && value.includes(")")) {
107+
108+
// Check if value is a string before calling .includes()
109+
if (typeof value === "string" && value.includes("(") && value.includes(")")) {
53110
value = value.split("(")[1].split(")")[0];
54111
}
112+
55113
return (value as string) || null;
56114
}
57115

platforms/blabsy-w3ds-auth-api/src/web3adapter/mappings/message.mapping.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"tableName": "message",
33
"schemaId": "550e8400-e29b-41d4-a716-446655440004",
4-
"ownerEnamePath": "user(senderId)",
4+
"ownerEnamePath": "chat(chat.ename)||user(senderId)",
55
"localToUniversalMap": {
66
"chatId": "chat(chatId),chatId",
77
"senderId": "user(senderId),senderId",

platforms/blabsy-w3ds-auth-api/src/web3adapter/watchers/firestoreWatcher.ts

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
CollectionReference,
66
CollectionGroup,
77
} from "firebase-admin/firestore";
8+
import { getFirestore } from "firebase-admin/firestore";
89
import path from "path";
910
import dotenv from "dotenv";
1011
import { adapter } from "../../controllers/WebhookController";
@@ -13,6 +14,7 @@ dotenv.config({ path: path.resolve(__dirname, "../../../../../.env") });
1314
export class FirestoreWatcher {
1415
private unsubscribe: (() => void) | null = null;
1516
private adapter = adapter;
17+
private db: FirebaseFirestore.Firestore;
1618
private isProcessing = false;
1719
private retryCount = 0;
1820
private readonly maxRetries: number = 3;
@@ -29,7 +31,9 @@ export class FirestoreWatcher {
2931
private readonly collection:
3032
| CollectionReference<DocumentData>
3133
| CollectionGroup<DocumentData>
32-
) {}
34+
) {
35+
this.db = getFirestore();
36+
}
3337

3438
async start(): Promise<void> {
3539
const collectionPath =
@@ -214,11 +218,69 @@ export class FirestoreWatcher {
214218

215219
const tableName = tableNameRaw.slice(0, tableNameRaw.length - 1);
216220

221+
// If this is a message, fetch and attach the full chat details
222+
let enrichedData: DocumentData & { id: string; chat?: DocumentData } = { ...data, id: doc.id };
223+
224+
console.log("----------------")
225+
console.log("----------------")
226+
console.log("----------------")
227+
console.log("tableName", tableName)
228+
console.log("----------------")
229+
console.log("----------------")
230+
console.log("----------------")
231+
console.log("----------------")
232+
233+
if (tableName === "message" && data.chatId) {
234+
235+
try {
236+
console.log(`Fetching chat details for message ${doc.id} in chat ${data.chatId}`);
237+
const chatDoc = await this.getChatDetails(data.chatId);
238+
if (chatDoc) {
239+
enrichedData = {
240+
...enrichedData,
241+
chat: chatDoc
242+
};
243+
console.log(`✅ Chat details attached to message ${doc.id}`);
244+
} else {
245+
console.log(`⚠️ Chat not found for message ${doc.id} in chat ${data.chatId}`);
246+
}
247+
} catch (error) {
248+
console.error(`❌ Error fetching chat details for message ${doc.id}:`, error);
249+
// Continue processing even if chat fetch fails
250+
}
251+
}
252+
217253
await this.adapter
218254
.handleChange({
219-
data: { ...data, id: doc.id },
255+
data: enrichedData,
220256
tableName,
221257
})
222258
.catch((e) => console.error(e));
223259
}
260+
261+
/**
262+
* Fetches the full chat details for a given chat ID
263+
*/
264+
private async getChatDetails(chatId: string): Promise<DocumentData | null> {
265+
try {
266+
// Fetch the chat document using the class's Firestore instance
267+
const chatDoc = await this.db.collection("chats").doc(chatId).get();
268+
269+
if (chatDoc.exists) {
270+
const chatData = chatDoc.data();
271+
if (chatData) {
272+
// Add the chat ID to the data
273+
return {
274+
...chatData,
275+
id: chatId
276+
};
277+
}
278+
}
279+
280+
return null;
281+
} catch (error) {
282+
console.error(`Error fetching chat details for chat ${chatId}:`, error);
283+
return null;
284+
}
285+
}
224286
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { MigrationInterface, QueryRunner } from "typeorm";
2+
3+
export class Migration1756065047421 implements MigrationInterface {
4+
name = 'Migration1756065047421'
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(`ALTER TABLE "group" ADD "ename" character varying`);
8+
}
9+
10+
public async down(queryRunner: QueryRunner): Promise<void> {
11+
await queryRunner.query(`ALTER TABLE "group" DROP COLUMN "ename"`);
12+
}
13+
14+
}

platforms/evoting-api/src/web3adapter/mappings/message.mapping.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"tableName": "messages",
33
"schemaId": "550e8400-e29b-41d4-a716-446655440004",
4-
"ownerEnamePath": "users(group.members[].ename)",
4+
"ownerEnamePath": "groups(group.ename)||users(group.members[].ename)",
55
"ownedJunctionTables": [],
66
"localToUniversalMap": {
77
"text": "content",

platforms/group-charter-manager-api/src/web3adapter/mappings/message.mapping.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"tableName": "messages",
33
"schemaId": "550e8400-e29b-41d4-a716-446655440004",
4-
"ownerEnamePath": "users(sender.ename)",
4+
"ownerEnamePath": "groups(group.ename)||users(sender.ename)",
55
"ownedJunctionTables": [],
66
"localToUniversalMap": {
77
"text": "content",

platforms/pictique-api/src/web3adapter/mappings/message.mapping.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"tableName": "messages",
33
"schemaId": "550e8400-e29b-41d4-a716-446655440004",
4-
"ownerEnamePath": "users(sender.ename)",
4+
"ownerEnamePath": "chats(chat.ename)||users(sender.ename)",
55
"localToUniversalMap": {
66
"chat": "chats(chat.id),chatId",
77
"text": "content",

0 commit comments

Comments
 (0)