Skip to content

Commit cec8c64

Browse files
authored
fix: modify about collect data by batch rtc_uuid (#853)
1 parent b5b0039 commit cec8c64

File tree

2 files changed

+48
-62
lines changed

2 files changed

+48
-62
lines changed

src/v1/controller/user/agreement/GetToRtc.ts

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -47,52 +47,49 @@ export class AgreementGetToRtc extends AbstractController<RequestType, ResponseT
4747
const rtcUidstr = this.querystring.uid;
4848
const room_uuid = this.querystring.room_uuid;
4949
const rtcUids = rtcUidstr.split(",");
50-
const listMap:Map<string, boolean> = new Map();
50+
const userAgreementMap:Map<string, boolean> = new Map(rtcUids.map(rtc_uid => [rtc_uid, false]));
5151
const length = rtcUids.length;
5252
if (length > 0) {
5353
let i = 0;
54-
const batchQueryRtcUids: string[][] = [];
5554
while (i < length) {
5655
const j = i + 50;
57-
batchQueryRtcUids.push(rtcUids.slice(i, j));
58-
i = j;
59-
}
60-
for (const rtc_uids of batchQueryRtcUids) {
61-
const roomUsersInfos = await dataSource
62-
.createQueryBuilder(RoomUserModel, "ru")
63-
.where("ru.room_uuid = :room_uuid", {
64-
room_uuid,
65-
})
66-
.andWhere("ru.rtc_uid IN (:...rtc_uids)", { rtc_uids })
67-
.getMany();
68-
69-
for (const rtc_uid of rtc_uids) {
70-
listMap.set(rtc_uid, false);
71-
}
72-
const collectInfos = await dataSource
73-
.createQueryBuilder(UserAgreementModel, "cInfo")
74-
.where("cInfo.user_uuid IN (:...user_uuid)", { user_uuid: roomUsersInfos.map(c=> c && c.user_uuid) })
75-
.getMany();
76-
77-
for (const rInfo of roomUsersInfos) {
78-
listMap.set(rInfo.rtc_uid, true);
79-
const rtc_uid = rInfo.rtc_uid;
80-
const user_uuid = rInfo.user_uuid;
81-
if (rtc_uid && user_uuid) {
82-
const cInfo = collectInfos.find(c=> c && (c.user_uuid === user_uuid));
83-
if (cInfo) {
84-
listMap.set(rtc_uid, cInfo.is_agree_collect_data);
85-
}
56+
const batchedRtcUids = rtcUids.slice(i, j);
57+
const roomUserInfos = await this.getRoomUserInfos(room_uuid, batchedRtcUids);
58+
const userUuids = roomUserInfos.map(user => user.user_uuid);
59+
const userAgreements = await this.getUserAgreements(userUuids);
60+
for (const userInfo of roomUserInfos) {
61+
const { rtc_uid, user_uuid } = userInfo;
62+
const userAgreement = userAgreements.find(ua => ua.user_uuid === user_uuid);
63+
if (userAgreement) {
64+
userAgreementMap.set(rtc_uid, userAgreement.is_agree_collect_data);
65+
} else {
66+
userAgreementMap.set(rtc_uid, true);
8667
}
8768
}
69+
i = j;
8870
}
8971
}
9072
return {
9173
status: Status.Success,
92-
data: Object.fromEntries(listMap)
74+
data: Object.fromEntries(userAgreementMap)
9375
}
9476
}
77+
9578

79+
private async getRoomUserInfos(room_uuid: string, rtc_uids: string[]): Promise<RoomUserModel[]> {
80+
return dataSource
81+
.createQueryBuilder(RoomUserModel, "ru")
82+
.where("ru.room_uuid = :room_uuid", { room_uuid })
83+
.andWhere("ru.rtc_uid IN (:...rtc_uids)", { rtc_uids })
84+
.getMany();
85+
}
86+
private async getUserAgreements(userUuids: string[]): Promise<UserAgreementModel[]> {
87+
return dataSource
88+
.createQueryBuilder(UserAgreementModel, "ua")
89+
.where("ua.user_uuid IN (:...userUuids)", { userUuids })
90+
.getMany();
91+
}
92+
9693
public errorHandler(error: Error): ResponseError {
9794
return this.autoHandlerError(error);
9895
}

src/v1/service/user/__tests__/userAgreement.test.ts

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -144,47 +144,36 @@ test(`${namespace} - get user by rtc_uuid collect agreement`, async ava => {
144144
ava.is(result?.user_uuid, result1?.user_uuid);
145145

146146
const rtcUids = [rtcUUID, rtcUUID1];
147+
const userAgreementMap:Map<string, boolean> = new Map(rtcUids.map(rtc_uid => [rtc_uid, false]));
147148
const length = rtcUids.length;
148-
const listMap:Map<string, boolean> = new Map();
149149
if (length > 0) {
150150
let i = 0;
151-
const batchQueryRtcUids: string[][] = [];
152151
while (i < length) {
153152
const j = i + 50;
154-
batchQueryRtcUids.push(rtcUids.slice(i, j));
155-
i = j;
156-
}
157-
for (const rtc_uids of batchQueryRtcUids) {
158-
const roomUsersInfos = await dataSource
153+
const batchedRtcUids = rtcUids.slice(i, j);
154+
const roomUserInfos = await dataSource
159155
.createQueryBuilder(RoomUserModel, "ru")
160-
.where("ru.room_uuid = :room_uuid", {
161-
room_uuid:roomUUID,
162-
})
163-
.andWhere("ru.rtc_uid IN (:...rtc_uids)", { rtc_uids })
156+
.where("ru.room_uuid = :room_uuid", { room_uuid:roomUUID })
157+
.andWhere("ru.rtc_uid IN (:...rtc_uids)", { rtc_uids: batchedRtcUids })
164158
.getMany();
165-
166-
for (const rtc_uid of rtc_uids) {
167-
listMap.set(rtc_uid, false);
168-
}
169-
const collectInfos = await dataSource
170-
.createQueryBuilder(UserAgreementModel, "cInfo")
171-
.where("cInfo.user_uuid IN (:...user_uuid)", { user_uuid: roomUsersInfos.map(c=> c && c.user_uuid) })
172-
.getMany();
173-
174-
for (const rInfo of roomUsersInfos) {
175-
listMap.set(rInfo.rtc_uid, true);
176-
const rtc_uid = rInfo.rtc_uid;
177-
const user_uuid = rInfo.user_uuid;
178-
if (rtc_uid && user_uuid) {
179-
const cInfo = collectInfos.find(c=> c && (c.user_uuid === user_uuid));
180-
if (cInfo) {
181-
listMap.set(rtc_uid, cInfo.is_agree_collect_data);
182-
}
159+
const userUuids = roomUserInfos.map(user => user.user_uuid);
160+
const userAgreements = await dataSource
161+
.createQueryBuilder(UserAgreementModel, "ua")
162+
.where("ua.user_uuid IN (:...userUuids)", { userUuids })
163+
.getMany();;
164+
for (const userInfo of roomUserInfos) {
165+
const { rtc_uid, user_uuid } = userInfo;
166+
const userAgreement = userAgreements.find(ua => ua.user_uuid === user_uuid);
167+
if (userAgreement) {
168+
userAgreementMap.set(rtc_uid, userAgreement.is_agree_collect_data);
169+
} else {
170+
userAgreementMap.set(rtc_uid, true);
183171
}
184172
}
173+
i = j;
185174
}
186175
}
187-
const obj = Object.fromEntries(listMap);
176+
const obj = Object.fromEntries(userAgreementMap);
188177

189178
ava.is(result1?.is_agree_collect_data, obj?.[rtcUUID]);
190179

0 commit comments

Comments
 (0)