diff --git a/src/v1/controller/user/agreement/Get.ts b/src/v1/controller/user/agreement/Get.ts index c7c47d24..1de0f5a0 100644 --- a/src/v1/controller/user/agreement/Get.ts +++ b/src/v1/controller/user/agreement/Get.ts @@ -11,8 +11,7 @@ import { ServiceUserAgreement } from "../../../service/user/UserAgreement"; auth: true, }) export class AgreementGet extends AbstractController { - public static readonly schema: FastifySchema = { - }; + public static readonly schema: FastifySchema = {}; public readonly svc: { userAgreement: ServiceUserAgreement; @@ -41,11 +40,7 @@ export class AgreementGet extends AbstractController } } -interface RequestType { - querystring: { - uid: string; - }; -} +interface RequestType {} interface ResponseType { isAgree: boolean; diff --git a/src/v1/controller/user/agreement/GetToRtc.ts b/src/v1/controller/user/agreement/GetToRtc.ts index 10823b9b..60b00c81 100644 --- a/src/v1/controller/user/agreement/GetToRtc.ts +++ b/src/v1/controller/user/agreement/GetToRtc.ts @@ -1,16 +1,19 @@ -import { RoomUserDAO } from "../../../../dao"; import { AbstractController, ControllerClassParams } from "../../../../abstract/controller"; import { Status } from "../../../../constants/Project"; import { Controller } from "../../../../decorator/Controller"; import { FastifySchema, Response, ResponseError } from "../../../../types/Server"; import { ServiceUserAgreement } from "../../../service/user/UserAgreement"; +import { RoomUserModel } from "../../../../model/room/RoomUser"; +import { dataSource } from "../../../../thirdPartyService/TypeORMService"; +import { UserAgreementModel } from "./../../../../model/user/Agreement"; @Controller({ method: "get", path: "private-polic/get", auth: false, - skipAutoHandle: true, + skipAutoHandle: false, + enable: true }) export class AgreementGetToRtc extends AbstractController { public static readonly schema: FastifySchema = { @@ -45,24 +48,42 @@ export class AgreementGetToRtc extends AbstractController = new Map(); - if (rtcUids.length > 0) { - for (const rtc_uid of rtcUids) { - const roomUserInfo = await RoomUserDAO().findOne(["user_uuid"], { - rtc_uid, - room_uuid - }); - if (roomUserInfo) { - const bol = await ServiceUserAgreement.hasCollectData(roomUserInfo.user_uuid); - if (bol) { - const isAgree = await ServiceUserAgreement.isAgreeCollectData(roomUserInfo.user_uuid); - listMap.set(rtc_uid, isAgree); - } else { - // 默认就是同意 - listMap.set(rtc_uid, true); + const length = rtcUids.length; + if (length > 0) { + let i = 0; + const batchQueryRtcUids: string[][] = []; + while (i < length) { + const j = i + 50; + batchQueryRtcUids.push(rtcUids.slice(i, j)); + i = j; + } + for (const rtc_uids of batchQueryRtcUids) { + const roomUsersInfos = await dataSource + .createQueryBuilder(RoomUserModel, "ru") + .where("ru.room_uuid = :room_uuid", { + room_uuid, + }) + .andWhere("ru.rtc_uid IN (:...rtc_uids)", { rtc_uids }) + .getMany(); + + for (const rtc_uid of rtc_uids) { + listMap.set(rtc_uid, false); + } + const collectInfos = await dataSource + .createQueryBuilder(UserAgreementModel, "cInfo") + .where("cInfo.user_uuid IN (:...user_uuid)", { user_uuid: roomUsersInfos.map(c=> c && c.user_uuid) }) + .getMany(); + + for (const rInfo of roomUsersInfos) { + listMap.set(rInfo.rtc_uid, true); + const rtc_uid = rInfo.rtc_uid; + const user_uuid = rInfo.user_uuid; + if (rtc_uid && user_uuid) { + const cInfo = collectInfos.find(c=> c && (c.user_uuid === user_uuid)); + if (cInfo) { + listMap.set(rtc_uid, cInfo.is_agree_collect_data); + } } - } else { - // 查不到用户则默认不同意 - listMap.set(rtc_uid, false); } } } diff --git a/src/v1/service/user/__tests__/userAgreement.test.ts b/src/v1/service/user/__tests__/userAgreement.test.ts index 14cb6b4d..7ef001f1 100644 --- a/src/v1/service/user/__tests__/userAgreement.test.ts +++ b/src/v1/service/user/__tests__/userAgreement.test.ts @@ -4,6 +4,8 @@ import { RoomUserDAO, UserAgreementDAO } from "../../../../dao"; import { v4 } from "uuid"; import { ServiceUserAgreement } from "../UserAgreement"; import cryptoRandomString from "crypto-random-string"; +import { RoomUserModel } from "../../../../model/room/RoomUser"; +import { UserAgreementModel } from "../../../../model/user/Agreement"; const namespace = "[service][service-user][service-user-agreement]"; @@ -110,6 +112,7 @@ test(`${namespace} - get user by rtc_uuid collect agreement`, async ava => { const userUUID = v4(); const roomUUID = v4(); const rtcUUID = cryptoRandomString({ length: 6, type: "numeric" }); + const rtcUUID1 = cryptoRandomString({ length: 6, type: "numeric" }); await Promise.all([ RoomUserDAO().insert({ @@ -132,7 +135,7 @@ test(`${namespace} - get user by rtc_uuid collect agreement`, async ava => { ava.is(result?.user_uuid, userUUID); - const result1 = await UserAgreementDAO().findOne(["user_uuid"], { + const result1 = await UserAgreementDAO().findOne(["user_uuid", "is_agree_collect_data"], { user_uuid: userUUID, }); @@ -140,4 +143,50 @@ test(`${namespace} - get user by rtc_uuid collect agreement`, async ava => { ava.is(result?.user_uuid, result1?.user_uuid); + const rtcUids = [rtcUUID, rtcUUID1]; + const length = rtcUids.length; + const listMap:Map = new Map(); + if (length > 0) { + let i = 0; + const batchQueryRtcUids: string[][] = []; + while (i < length) { + const j = i + 50; + batchQueryRtcUids.push(rtcUids.slice(i, j)); + i = j; + } + for (const rtc_uids of batchQueryRtcUids) { + const roomUsersInfos = await dataSource + .createQueryBuilder(RoomUserModel, "ru") + .where("ru.room_uuid = :room_uuid", { + room_uuid:roomUUID, + }) + .andWhere("ru.rtc_uid IN (:...rtc_uids)", { rtc_uids }) + .getMany(); + + for (const rtc_uid of rtc_uids) { + listMap.set(rtc_uid, false); + } + const collectInfos = await dataSource + .createQueryBuilder(UserAgreementModel, "cInfo") + .where("cInfo.user_uuid IN (:...user_uuid)", { user_uuid: roomUsersInfos.map(c=> c && c.user_uuid) }) + .getMany(); + + for (const rInfo of roomUsersInfos) { + listMap.set(rInfo.rtc_uid, true); + const rtc_uid = rInfo.rtc_uid; + const user_uuid = rInfo.user_uuid; + if (rtc_uid && user_uuid) { + const cInfo = collectInfos.find(c=> c && (c.user_uuid === user_uuid)); + if (cInfo) { + listMap.set(rtc_uid, cInfo.is_agree_collect_data); + } + } + } + } + } + const obj = Object.fromEntries(listMap); + + ava.is(result1?.is_agree_collect_data, obj?.[rtcUUID]); + + ava.is(false, obj?.[rtcUUID1]); }); \ No newline at end of file