forked from Yara724/api
Fixed GET users
This commit is contained in:
87
src/helpers/party-access-queries.ts
Normal file
87
src/helpers/party-access-queries.ts
Normal file
@@ -0,0 +1,87 @@
|
||||
import { Types } from "mongoose";
|
||||
import { RoleEnum } from "src/Types&Enums/role.enum";
|
||||
import { iranMobileLookupVariants } from "./iran-mobile";
|
||||
|
||||
/**
|
||||
* Mongo filter matching a user id field stored as ObjectId or legacy string.
|
||||
*/
|
||||
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 })) };
|
||||
}
|
||||
|
||||
/** `$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>[] {
|
||||
const or: Record<string, unknown>[] = [];
|
||||
const userIdCond = user?.sub
|
||||
? mongoUserIdEqualityFilter("parties.person.userId", user.sub)
|
||||
: null;
|
||||
if (userIdCond) or.push(userIdCond);
|
||||
|
||||
const phoneVariants = user?.username
|
||||
? iranMobileLookupVariants(user.username)
|
||||
: [];
|
||||
if (phoneVariants.length > 0) {
|
||||
or.push({ "parties.person.phoneNumber": { $in: phoneVariants } });
|
||||
}
|
||||
return or;
|
||||
}
|
||||
|
||||
/** `$or` branches: user owns a claim or is tied via blame party membership. */
|
||||
export function buildClaimOwnerAccessOrConditions(
|
||||
userSub: string,
|
||||
username?: string,
|
||||
): Record<string, unknown>[] {
|
||||
const or: Record<string, unknown>[] = [];
|
||||
const ownerIdCond = mongoUserIdEqualityFilter("owner.userId", userSub);
|
||||
if (ownerIdCond) or.push(ownerIdCond);
|
||||
return or;
|
||||
}
|
||||
|
||||
export function buildFieldExpertInitiatedBlameFilter(
|
||||
expertSub: string,
|
||||
): Record<string, unknown> {
|
||||
const expertOid = Types.ObjectId.isValid(expertSub)
|
||||
? new Types.ObjectId(expertSub)
|
||||
: expertSub;
|
||||
return {
|
||||
expertInitiated: true,
|
||||
initiatedByFieldExpertId: expertOid,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildFieldExpertClaimAccessOrConditions(
|
||||
expertSub: string,
|
||||
expertBlameIds: unknown[],
|
||||
): Record<string, unknown>[] {
|
||||
const expertOid = Types.ObjectId.isValid(expertSub)
|
||||
? new Types.ObjectId(expertSub)
|
||||
: expertSub;
|
||||
const or: Record<string, unknown>[] = [
|
||||
{ initiatedByFieldExpertId: expertOid },
|
||||
];
|
||||
if (expertBlameIds.length > 0) {
|
||||
or.push({ blameRequestId: { $in: expertBlameIds } });
|
||||
}
|
||||
return or;
|
||||
}
|
||||
|
||||
/** True when actor is the field expert who initiated this blame (any JWT shape). */
|
||||
export function actorIsFieldExpertInitiator(
|
||||
actor: { sub?: string; role?: string } | null | undefined,
|
||||
): boolean {
|
||||
return actor?.role === RoleEnum.FIELD_EXPERT && !!actor?.sub;
|
||||
}
|
||||
Reference in New Issue
Block a user