2026-08-29 21:06:30 +03:30
|
|
|
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
|
|
|
|
import { ConfigService } from '@nestjs/config';
|
|
|
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
|
|
|
import { ExtractJwt, Strategy } from 'passport-jwt';
|
2026-09-26 17:15:09 +03:30
|
|
|
import type { DataVisibility } from '@mento/shared';
|
2026-09-20 11:24:14 +03:30
|
|
|
import { BillingService } from '../billing/billing.service';
|
2026-09-26 17:15:09 +03:30
|
|
|
import { normalizeDataVisibility } from '../common/record-scope';
|
2026-09-15 17:33:05 +03:30
|
|
|
import { PrismaService } from '../prisma/prisma.service';
|
2026-09-26 17:15:09 +03:30
|
|
|
import { canViewAllRecords } from '@mento/shared';
|
2026-08-29 21:06:30 +03:30
|
|
|
|
|
|
|
|
export type JwtPayload = {
|
|
|
|
|
sub: string;
|
|
|
|
|
organizationId: string;
|
|
|
|
|
role: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class JwtStrategy extends PassportStrategy(Strategy) {
|
2026-09-15 17:33:05 +03:30
|
|
|
constructor(
|
|
|
|
|
config: ConfigService,
|
|
|
|
|
private readonly prisma: PrismaService,
|
2026-09-20 11:24:14 +03:30
|
|
|
private readonly billing: BillingService,
|
2026-09-15 17:33:05 +03:30
|
|
|
) {
|
2026-08-29 21:06:30 +03:30
|
|
|
super({
|
|
|
|
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
|
|
|
|
ignoreExpiration: false,
|
|
|
|
|
secretOrKey: config.get<string>('JWT_SECRET') ?? 'dev-secret',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-15 17:33:05 +03:30
|
|
|
async validate(payload: JwtPayload & { typ?: string; mobile?: string }) {
|
2026-09-19 21:54:21 +03:30
|
|
|
if (
|
|
|
|
|
payload.typ === 'portal' ||
|
|
|
|
|
payload.typ === 'org-select' ||
|
|
|
|
|
payload.typ === 'platform-admin'
|
|
|
|
|
) {
|
2026-09-15 21:37:58 +03:30
|
|
|
throw new UnauthorizedException();
|
|
|
|
|
}
|
|
|
|
|
if (!payload.sub || !payload.organizationId) {
|
2026-08-29 21:06:30 +03:30
|
|
|
throw new UnauthorizedException();
|
|
|
|
|
}
|
2026-09-15 17:33:05 +03:30
|
|
|
const user = await this.prisma.user.findUnique({
|
|
|
|
|
where: { id: payload.sub },
|
2026-09-15 21:37:58 +03:30
|
|
|
select: { id: true, isActive: true },
|
2026-09-15 17:33:05 +03:30
|
|
|
});
|
|
|
|
|
if (!user || user.isActive === false) {
|
|
|
|
|
throw new UnauthorizedException();
|
|
|
|
|
}
|
2026-09-15 21:37:58 +03:30
|
|
|
const membership = await this.prisma.membership.findFirst({
|
|
|
|
|
where: {
|
|
|
|
|
userId: user.id,
|
|
|
|
|
organizationId: payload.organizationId,
|
|
|
|
|
isActive: true,
|
2026-09-19 21:54:21 +03:30
|
|
|
organization: { isActive: true },
|
2026-09-15 21:37:58 +03:30
|
|
|
},
|
2026-09-26 17:15:09 +03:30
|
|
|
select: { organizationId: true, role: true, dataVisibility: true },
|
2026-09-15 21:37:58 +03:30
|
|
|
});
|
|
|
|
|
if (!membership) {
|
|
|
|
|
throw new UnauthorizedException();
|
|
|
|
|
}
|
2026-09-20 11:24:14 +03:30
|
|
|
await this.billing.requireActiveForOrganization(membership.organizationId);
|
2026-09-26 17:15:09 +03:30
|
|
|
|
|
|
|
|
const dataVisibility = normalizeDataVisibility(membership.dataVisibility);
|
|
|
|
|
const visibleUserIds = await this.resolveVisibleUserIds(
|
|
|
|
|
membership.organizationId,
|
|
|
|
|
user.id,
|
|
|
|
|
membership.role,
|
|
|
|
|
dataVisibility,
|
|
|
|
|
);
|
|
|
|
|
|
2026-08-29 21:06:30 +03:30
|
|
|
return {
|
2026-09-15 17:33:05 +03:30
|
|
|
userId: user.id,
|
2026-09-15 21:37:58 +03:30
|
|
|
organizationId: membership.organizationId,
|
|
|
|
|
role: membership.role,
|
2026-09-26 17:15:09 +03:30
|
|
|
dataVisibility,
|
|
|
|
|
visibleUserIds,
|
2026-08-29 21:06:30 +03:30
|
|
|
};
|
|
|
|
|
}
|
2026-09-26 17:15:09 +03:30
|
|
|
|
|
|
|
|
private async resolveVisibleUserIds(
|
|
|
|
|
organizationId: string,
|
|
|
|
|
userId: string,
|
|
|
|
|
role: string,
|
|
|
|
|
dataVisibility: DataVisibility,
|
|
|
|
|
): Promise<string[] | null> {
|
|
|
|
|
if (canViewAllRecords(role) || dataVisibility === 'org') {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (dataVisibility === 'own') {
|
|
|
|
|
return [userId];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const memberships = await this.prisma.teamMember.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
userId,
|
|
|
|
|
team: { organizationId },
|
|
|
|
|
},
|
|
|
|
|
select: { teamId: true },
|
|
|
|
|
});
|
|
|
|
|
if (!memberships.length) return [userId];
|
|
|
|
|
|
|
|
|
|
const peers = await this.prisma.teamMember.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
teamId: { in: memberships.map((m) => m.teamId) },
|
|
|
|
|
},
|
|
|
|
|
select: { userId: true },
|
|
|
|
|
});
|
|
|
|
|
return [...new Set([userId, ...peers.map((p) => p.userId)])];
|
|
|
|
|
}
|
2026-08-29 21:06:30 +03:30
|
|
|
}
|