forked from Yara724/api
Fixed users not being able to view their files
This commit is contained in:
@@ -2,6 +2,36 @@ import { Types } from "mongoose";
|
||||
import { RoleEnum } from "src/Types&Enums/role.enum";
|
||||
import { iranMobileLookupVariants } from "./iran-mobile";
|
||||
|
||||
/** ObjectId + string variants for one or more user ids (legacy duplicate accounts). */
|
||||
export function collectUserIdVariants(
|
||||
...ids: (string | Types.ObjectId | undefined | null)[]
|
||||
): unknown[] {
|
||||
const seen = new Set<string>();
|
||||
const out: unknown[] = [];
|
||||
for (const id of ids) {
|
||||
if (id == null || id === "") continue;
|
||||
const s = String(id);
|
||||
if (seen.has(s)) continue;
|
||||
seen.add(s);
|
||||
out.push(s);
|
||||
if (Types.ObjectId.isValid(s)) {
|
||||
out.push(new Types.ObjectId(s));
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function buildUserIdFieldOrFilter(
|
||||
fieldPath: string,
|
||||
idVariants: unknown[],
|
||||
): Record<string, unknown> | null {
|
||||
if (!idVariants.length) return null;
|
||||
if (idVariants.length === 1) {
|
||||
return { [fieldPath]: idVariants[0] };
|
||||
}
|
||||
return { $or: idVariants.map((id) => ({ [fieldPath]: id })) };
|
||||
}
|
||||
|
||||
/**
|
||||
* Mongo filter matching a user id field stored as ObjectId or legacy string.
|
||||
*/
|
||||
@@ -9,26 +39,23 @@ export function mongoUserIdEqualityFilter(
|
||||
fieldPath: string,
|
||||
userSub: string,
|
||||
): Record<string, unknown> | null {
|
||||
if (!userSub) return null;
|
||||
const variants: unknown[] = [userSub];
|
||||
if (Types.ObjectId.isValid(userSub)) {
|
||||
variants.push(new Types.ObjectId(userSub));
|
||||
}
|
||||
if (variants.length === 1) {
|
||||
return { [fieldPath]: variants[0] };
|
||||
}
|
||||
return { $or: variants.map((v) => ({ [fieldPath]: v })) };
|
||||
return buildUserIdFieldOrFilter(fieldPath, collectUserIdVariants(userSub));
|
||||
}
|
||||
|
||||
/** `$or` branches: user is a party on a blame case (by userId and/or phone). */
|
||||
export function buildBlamePartyAccessOrConditions(user: {
|
||||
sub?: string;
|
||||
username?: string;
|
||||
}): Record<string, unknown>[] {
|
||||
export function buildBlamePartyAccessOrConditions(
|
||||
user: {
|
||||
sub?: string;
|
||||
username?: string;
|
||||
},
|
||||
linkedUserIdStrings: string[] = [],
|
||||
): Record<string, unknown>[] {
|
||||
const or: Record<string, unknown>[] = [];
|
||||
const userIdCond = user?.sub
|
||||
? mongoUserIdEqualityFilter("parties.person.userId", user.sub)
|
||||
: null;
|
||||
const idVariants = collectUserIdVariants(user.sub, ...linkedUserIdStrings);
|
||||
const userIdCond = buildUserIdFieldOrFilter(
|
||||
"parties.person.userId",
|
||||
idVariants,
|
||||
);
|
||||
if (userIdCond) or.push(userIdCond);
|
||||
|
||||
const phoneVariants = user?.username
|
||||
|
||||
Reference in New Issue
Block a user