Skip to content

Commit 3e74892

Browse files
committed
chore: fix logical issue here
1 parent ab55a16 commit 3e74892

File tree

2 files changed

+50
-30
lines changed

2 files changed

+50
-30
lines changed

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

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class WebhookController {
3232
const schemaId = req.body.schemaId;
3333
const globalId = req.body.id;
3434
const mapping = Object.values(this.adapter.mapping).find(
35-
(m) => m.schemaId === schemaId,
35+
(m) => m.schemaId === schemaId
3636
);
3737
this.adapter.addToLockedIds(globalId);
3838

@@ -47,20 +47,38 @@ export class WebhookController {
4747
let localId = await this.adapter.mappingDb.getLocalId(globalId);
4848

4949
if (mapping.tableName === "users") {
50-
const { user } = await this.userService.findOrCreateUser(
51-
req.body.w3id,
52-
);
53-
for (const key of Object.keys(local.data)) {
54-
// @ts-ignore
55-
user[key] = local.data[key];
50+
if (localId) {
51+
const user = await this.userService.findById(localId);
52+
for (const key of Object.keys(local.data)) {
53+
// @ts-ignore
54+
user[key] = local.data[key];
55+
}
56+
if (!user) throw new Error();
57+
user.name = req.body.data.displayName;
58+
await this.userService.userRepository.save(user);
59+
await this.adapter.mappingDb.storeMapping({
60+
localId: user.id,
61+
globalId: req.body.id,
62+
});
63+
this.adapter.addToLockedIds(user.id);
64+
this.adapter.addToLockedIds(globalId);
65+
} else {
66+
const { user } = await this.userService.findOrCreateUser(
67+
req.body.w3id
68+
);
69+
for (const key of Object.keys(local.data)) {
70+
// @ts-ignore
71+
user[key] = local.data[key];
72+
}
73+
user.name = req.body.data.displayName;
74+
await this.userService.userRepository.save(user);
75+
await this.adapter.mappingDb.storeMapping({
76+
localId: user.id,
77+
globalId: req.body.id,
78+
});
79+
this.adapter.addToLockedIds(user.id);
80+
this.adapter.addToLockedIds(globalId);
5681
}
57-
user.name = req.body.data.displayName;
58-
await this.userService.userRepository.save(user);
59-
await this.adapter.mappingDb.storeMapping({
60-
localId: user.id,
61-
globalId: req.body.id,
62-
});
63-
this.adapter.addToLockedIds(user.id);
6482
} else if (mapping.tableName === "posts") {
6583
let author: User | null = null;
6684
if (local.data.author) {
@@ -79,10 +97,10 @@ export class WebhookController {
7997
return await this.userService.findById(userId);
8098
}
8199
return null;
82-
},
100+
}
83101
);
84102
likedBy = (await Promise.all(likedByPromises)).filter(
85-
(user): user is User => user !== null,
103+
(user): user is User => user !== null
86104
);
87105
}
88106

@@ -100,13 +118,13 @@ export class WebhookController {
100118
comment.author = author as User;
101119
comment.post = parent as Post;
102120
await this.commentService.commentRepository.save(
103-
comment,
121+
comment
104122
);
105123
} else {
106124
const comment = await this.commentService.createComment(
107125
parent?.id as string,
108126
author?.id as string,
109-
local.data.text as string,
127+
local.data.text as string
110128
);
111129
localId = comment.id;
112130
await this.adapter.mappingDb.storeMapping({
@@ -128,14 +146,14 @@ export class WebhookController {
128146
.split("(")[1]
129147
.split(")")[0];
130148
return await this.userService.findById(
131-
userId,
149+
userId
132150
);
133151
}
134152
return null;
135-
},
153+
}
136154
);
137155
likedBy = (await Promise.all(likedByPromises)).filter(
138-
(user): user is User => user !== null,
156+
(user): user is User => user !== null
139157
);
140158
}
141159

@@ -160,7 +178,7 @@ export class WebhookController {
160178
{
161179
...local.data,
162180
likedBy,
163-
},
181+
}
164182
);
165183

166184
this.adapter.addToLockedIds(post.id);
@@ -192,7 +210,7 @@ export class WebhookController {
192210
return await this.userService.findById(userId);
193211
}
194212
return null;
195-
},
213+
}
196214
);
197215
participants = (
198216
await Promise.all(participantPromises)
@@ -212,7 +230,7 @@ export class WebhookController {
212230
} else {
213231
const chat = await this.chatService.createChat(
214232
local.data.name as string,
215-
participants.map((p) => p.id),
233+
participants.map((p) => p.id)
216234
);
217235

218236
this.adapter.addToLockedIds(chat.id);
@@ -262,7 +280,7 @@ export class WebhookController {
262280
const message = await this.chatService.sendMessage(
263281
chat.id,
264282
sender.id,
265-
local.data.text as string,
283+
local.data.text as string
266284
);
267285

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

platforms/pictique-api/src/services/UserService.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class UserService {
2020
}
2121

2222
async findOrCreateUser(
23-
ename: string,
23+
ename: string
2424
): Promise<{ user: User; token: string }> {
2525
let user = await this.userRepository.findOne({
2626
where: { ename },
@@ -39,11 +39,11 @@ export class UserService {
3939
}
4040

4141
searchUsers = async (query: string) => {
42-
const searchQuery = query.toLowerCase();
42+
const searchQuery = query;
4343

4444
return this.userRepository.find({
4545
where: [
46-
{ handle: Like(`%${searchQuery}%`) },
46+
{ name: Like(`%${searchQuery}%`) },
4747
{ ename: Like(`%${searchQuery}%`) },
4848
],
4949
select: {
@@ -126,7 +126,10 @@ export class UserService {
126126
};
127127
}
128128

129-
async updateProfile(userId: string, data: { handle?: string; avatarUrl?: string; name?: string }): Promise<User> {
129+
async updateProfile(
130+
userId: string,
131+
data: { handle?: string; avatarUrl?: string; name?: string }
132+
): Promise<User> {
130133
const user = await this.userRepository.findOneBy({ id: userId });
131134
if (!user) {
132135
throw new Error("User not found");
@@ -140,4 +143,3 @@ export class UserService {
140143
return await this.userRepository.save(user);
141144
}
142145
}
143-

0 commit comments

Comments
 (0)