| 137 | } |
| 138 | |
| 139 | async function runInDomain(domainId: string, report: Report) { |
| 140 | const results: Record<keyof typeof RpTypes, ND> = {}; |
| 141 | const udict = Counter(); |
| 142 | await db.collection('domain.user').updateMany({ domainId }, { $set: { rpInfo: {} } }); |
| 143 | for (const type in RpTypes) { |
| 144 | results[type] = new Proxy({}, { get: (self, key) => self[key] || RpTypes[type].base }); |
| 145 | await RpTypes[type].run([domainId], results[type], report); |
| 146 | const bulk = db.collection('domain.user').initializeUnorderedBulkOp(); |
| 147 | for (const uid in results[type]) { |
| 148 | const udoc = await UserModel.getById(domainId, +uid); |
| 149 | if (!udoc?.hasPriv(PRIV.PRIV_USER_PROFILE)) continue; |
| 150 | bulk.find({ domainId, uid: +uid }).updateOne({ $set: { [`rpInfo.${type}`]: results[type][uid] } }); |
| 151 | udict[+uid] += results[type][uid]; |
| 152 | } |
| 153 | if (bulk.batches.length) await bulk.execute(); |
| 154 | } |
| 155 | await domain.setMultiUserInDomain(domainId, {}, { rp: 0 }); |
| 156 | const bulk = db.collection('domain.user').initializeUnorderedBulkOp(); |
| 157 | for (const uid in udict) { |
| 158 | bulk.find({ domainId, uid: +uid }).upsert().update({ $set: { rp: Math.max(0, udict[uid]) } }); |
| 159 | } |
| 160 | if (bulk.batches.length) await bulk.execute(); |
| 161 | await calcLevel(domainId, report); |
| 162 | } |
| 163 | |
| 164 | export async function run({ domainId }, report: Report) { |
| 165 | if (!domainId) { |