Skip to content

Commit d6d3afb

Browse files
committed
feat: fix issue with posts
1 parent 9f457ac commit d6d3afb

File tree

3 files changed

+40
-36
lines changed

3 files changed

+40
-36
lines changed

infrastructure/web3-adapter/src/db/mapping.db.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ export class MappingDatabase {
5252
}): Promise<void> {
5353
// Validate inputs
5454
if (!params.localId || !params.globalId || !params.tableName) {
55-
throw new Error("Invalid mapping parameters: all fields are required");
55+
throw new Error(
56+
"Invalid mapping parameters: all fields are required",
57+
);
5658
}
5759

5860
// Check if mapping already exists
@@ -69,10 +71,9 @@ export class MappingDatabase {
6971
await this.runAsync(
7072
`INSERT INTO id_mappings (local_id, global_id, table_name)
7173
VALUES (?, ?, ?)`,
72-
[params.localId, params.globalId, params.tableName]
74+
[params.localId, params.globalId, params.tableName],
7375
);
7476

75-
// Verify the mapping was stored
7677
const storedMapping = await this.getGlobalId({
7778
localId: params.localId,
7879
tableName: params.tableName,
@@ -102,7 +103,7 @@ export class MappingDatabase {
102103
`SELECT global_id
103104
FROM id_mappings
104105
WHERE local_id = ? AND table_name = ?`,
105-
[params.localId, params.tableName]
106+
[params.localId, params.tableName],
106107
);
107108
return result?.global_id ?? null;
108109
} catch (error) {
@@ -127,7 +128,7 @@ export class MappingDatabase {
127128
`SELECT local_id
128129
FROM id_mappings
129130
WHERE global_id = ? AND table_name = ?`,
130-
[params.globalId, params.tableName]
131+
[params.globalId, params.tableName],
131132
);
132133
return result?.local_id ?? null;
133134
} catch (error) {
@@ -150,7 +151,7 @@ export class MappingDatabase {
150151
await this.runAsync(
151152
`DELETE FROM id_mappings
152153
WHERE local_id = ? AND table_name = ?`,
153-
[params.localId, params.tableName]
154+
[params.localId, params.tableName],
154155
);
155156
} catch (error) {
156157
throw error;
@@ -160,10 +161,12 @@ export class MappingDatabase {
160161
/**
161162
* Get all mappings for a table
162163
*/
163-
public async getTableMappings(tableName: string): Promise<Array<{
164-
localId: string;
165-
globalId: string;
166-
}>> {
164+
public async getTableMappings(tableName: string): Promise<
165+
Array<{
166+
localId: string;
167+
globalId: string;
168+
}>
169+
> {
167170
if (!tableName) {
168171
return [];
169172
}
@@ -173,7 +176,7 @@ export class MappingDatabase {
173176
`SELECT local_id, global_id
174177
FROM id_mappings
175178
WHERE table_name = ?`,
176-
[tableName]
179+
[tableName],
177180
);
178181

179182
return results.map(({ local_id, global_id }) => ({
@@ -196,4 +199,3 @@ export class MappingDatabase {
196199
}
197200
}
198201
}
199-

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export function getValueByPath(obj: Record<string, any>, path: string): any {
1313
// If there's a field path after [], map through the array
1414
if (fieldPath) {
1515
return array.map((item) =>
16-
getValueByPath(item, fieldPath.slice(1))
16+
getValueByPath(item, fieldPath.slice(1)),
1717
); // Remove the leading dot
1818
}
1919

@@ -30,7 +30,7 @@ export function getValueByPath(obj: Record<string, any>, path: string): any {
3030

3131
async function extractOwnerEvault(
3232
data: Record<string, unknown>,
33-
ownerEnamePath: string
33+
ownerEnamePath: string,
3434
): Promise<string | null> {
3535
if (!ownerEnamePath || ownerEnamePath === "null") {
3636
return null;
@@ -56,7 +56,7 @@ export async function fromGlobal({
5656
const result: Record<string, unknown> = {};
5757

5858
for (let [localKey, globalPathRaw] of Object.entries(
59-
mapping.localToUniversalMap
59+
mapping.localToUniversalMap,
6060
)) {
6161
let value: any;
6262
let targetKey: string = localKey;
@@ -71,7 +71,7 @@ export async function fromGlobal({
7171
if (calcMatch) {
7272
const calcResult = evaluateCalcExpression(
7373
calcMatch[1],
74-
data
74+
data,
7575
);
7676
value =
7777
calcResult !== undefined
@@ -115,7 +115,7 @@ export async function fromGlobal({
115115
});
116116

117117
return localId ? `${tableRef}(${localId})` : null;
118-
})
118+
}),
119119
);
120120
} else {
121121
value = await mappingStore.getLocalId({
@@ -136,7 +136,7 @@ export async function fromGlobal({
136136

137137
function evaluateCalcExpression(
138138
expr: string,
139-
context: Record<string, any>
139+
context: Record<string, any>,
140140
): number | undefined {
141141
const tokens = expr
142142
.split(/[^\w.]+/)
@@ -149,7 +149,7 @@ function evaluateCalcExpression(
149149
if (typeof value !== "undefined") {
150150
resolvedExpr = resolvedExpr.replace(
151151
new RegExp(`\\b${token.replace(".", "\\.")}\\b`, "g"),
152-
value
152+
value,
153153
);
154154
}
155155
}
@@ -169,7 +169,7 @@ export async function toGlobal({
169169
const result: Record<string, unknown> = {};
170170

171171
for (let [localKey, globalPathRaw] of Object.entries(
172-
mapping.localToUniversalMap
172+
mapping.localToUniversalMap,
173173
)) {
174174
let value: any;
175175
let targetKey: string = globalPathRaw;
@@ -201,7 +201,7 @@ export async function toGlobal({
201201
if (calcMatch) {
202202
const calcResult = evaluateCalcExpression(
203203
calcMatch[1],
204-
data
204+
data,
205205
);
206206
value =
207207
calcResult !== undefined
@@ -262,8 +262,8 @@ export async function toGlobal({
262262
(await mappingStore.getGlobalId({
263263
localId: v,
264264
tableName: tableRef,
265-
})) ?? undefined
266-
)
265+
})) ?? undefined,
266+
),
267267
);
268268
} else {
269269
value =

platforms/pictique-api/src/controllers/WebhookController.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class WebhookController {
3131
const schemaId = req.body.schemaId;
3232
const globalId = req.body.id;
3333
const mapping = Object.values(this.adapter.mapping).find(
34-
(m) => m.schemaId === schemaId
34+
(m) => m.schemaId === schemaId,
3535
);
3636

3737
if (!mapping) throw new Error();
@@ -40,14 +40,16 @@ export class WebhookController {
4040
mapping,
4141
});
4242

43+
mapping.tableName =
44+
mapping.tableName === "comments" ? "posts" : mapping.tableName;
4345
let localId = await this.adapter.mappingDb.getLocalId({
4446
globalId,
4547
tableName: mapping.tableName,
4648
});
4749

4850
if (mapping.tableName === "users") {
4951
const { user } = await this.userService.findOrCreateUser(
50-
req.body.w3id
52+
req.body.w3id,
5153
);
5254
for (const key of Object.keys(local.data)) {
5355
// @ts-ignore
@@ -79,10 +81,10 @@ export class WebhookController {
7981
return await this.userService.findById(userId);
8082
}
8183
return null;
82-
}
84+
},
8385
);
8486
likedBy = (await Promise.all(likedByPromises)).filter(
85-
(user): user is User => user !== null
87+
(user): user is User => user !== null,
8688
);
8789
}
8890

@@ -100,13 +102,13 @@ export class WebhookController {
100102
comment.author = author as User;
101103
comment.post = parent as Post;
102104
await this.commentService.commentRepository.save(
103-
comment
105+
comment,
104106
);
105107
} else {
106108
const comment = await this.commentService.createComment(
107109
parent?.id as string,
108110
author?.id as string,
109-
local.data.text as string
111+
local.data.text as string,
110112
);
111113
localId = comment.id;
112114
await this.adapter.mappingDb.storeMapping({
@@ -129,14 +131,14 @@ export class WebhookController {
129131
.split("(")[1]
130132
.split(")")[0];
131133
return await this.userService.findById(
132-
userId
134+
userId,
133135
);
134136
}
135137
return null;
136-
}
138+
},
137139
);
138140
likedBy = (await Promise.all(likedByPromises)).filter(
139-
(user): user is User => user !== null
141+
(user): user is User => user !== null,
140142
);
141143
}
142144

@@ -161,7 +163,7 @@ export class WebhookController {
161163
{
162164
...local.data,
163165
likedBy,
164-
}
166+
},
165167
);
166168

167169
this.adapter.addToLockedIds(post.id);
@@ -196,7 +198,7 @@ export class WebhookController {
196198
return await this.userService.findById(userId);
197199
}
198200
return null;
199-
}
201+
},
200202
);
201203
participants = (
202204
await Promise.all(participantPromises)
@@ -215,7 +217,7 @@ export class WebhookController {
215217
} else {
216218
const chat = await this.chatService.createChat(
217219
local.data.name as string,
218-
participants.map((p) => p.id)
220+
participants.map((p) => p.id),
219221
);
220222

221223
this.adapter.addToLockedIds(chat.id);
@@ -266,7 +268,7 @@ export class WebhookController {
266268
const message = await this.chatService.sendMessage(
267269
chat.id,
268270
sender.id,
269-
local.data.text as string
271+
local.data.text as string,
270272
);
271273

272274
this.adapter.addToLockedIds(message.id);

0 commit comments

Comments
 (0)