Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | 14x 14x 14x 14x 22x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 1x 1x | import {
Injectable,
NotFoundException,
BadRequestException,
} from '@nestjs/common';
import { PrismaService } from '@app/modules/prisma/prisma.service';
import {
ApplicationStatus,
ModerationStatus,
ModerationType,
Prisma,
Role,
} from '@prisma/client';
import { RejectDto } from '@app/modules/admin/dto/reject.dto';
@Injectable()
export class AdminService {
constructor(private readonly prisma: PrismaService) {}
getAgencyRequests() {
return this.prisma.client.moderationQueue.findMany({
where: {
type: ModerationType.AGENCY_APPLICATION,
status: ModerationStatus.PENDING,
},
include: {
submittedBy: {
select: { id: true, name: true, email: true },
},
agencyApplication: true,
},
orderBy: { createdAt: 'asc' },
});
}
async approveAgencyRequest(moderationId: number, adminId: number) {
return this.prisma.client.$transaction(async (tx: Prisma.TransactionClient) => {
const moderationItem = await tx.moderationQueue.findUnique({
where: { id: moderationId },
include: { agencyApplication: true },
});
Iif (
!moderationItem ||
moderationItem.status !== ModerationStatus.PENDING ||
moderationItem.type !== ModerationType.AGENCY_APPLICATION
) {
throw new NotFoundException(
'Pending agency creation request not found.',
);
}
const moderatedContent =
moderationItem.moderatedContent as Prisma.JsonObject;
const agencyName = moderatedContent.agencyName as string;
Iif (!agencyName) {
throw new BadRequestException(
'Agency name is missing in moderation payload.',
);
}
// 1. Create the Agency
const newAgency = await tx.agency.create({
data: {
name: agencyName,
ownerId: moderationItem.submittedById,
isVerified: true,
},
});
// 2. Update the User's role and assign them to the new agency
await tx.user.update({
where: { id: moderationItem.submittedById },
data: {
agencyId: newAgency.id,
role: Role.AGENT,
},
});
// 3. Update the application status
if (moderationItem.agencyApplication) {
await tx.agencyApplication.update({
where: { id: moderationItem.agencyApplication.id },
data: { status: ApplicationStatus.APPROVED, agencyId: newAgency.id },
});
}
// 4. Update moderation item
return tx.moderationQueue.update({
where: { id: moderationId },
data: {
status: ModerationStatus.APPROVED,
reviewedById: adminId,
agencyId: newAgency.id,
},
});
});
}
async rejectAgencyRequest(
moderationId: number,
adminId: number,
{ reason }: RejectDto,
) {
return this.prisma.client.$transaction(async (tx: Prisma.TransactionClient) => {
const moderationItem = await tx.moderationQueue.findUnique({
where: { id: moderationId },
include: { agencyApplication: true },
});
Iif (
!moderationItem ||
moderationItem.status !== ModerationStatus.PENDING ||
moderationItem.type !== ModerationType.AGENCY_APPLICATION
) {
throw new NotFoundException(
'Pending agency creation request not found.',
);
}
if (moderationItem.agencyApplication) {
await tx.agencyApplication.update({
where: { id: moderationItem.agencyApplication.id },
data: { status: ApplicationStatus.REJECTED },
});
}
return tx.moderationQueue.update({
where: { id: moderationId },
data: {
status: ModerationStatus.REJECTED,
reviewedById: adminId,
reason,
},
});
});
}
getAgencyUpdateRequests() {
return this.prisma.client.moderationQueue.findMany({
where: {
type: ModerationType.AGENCY_PROFILE_UPDATE,
status: ModerationStatus.PENDING,
},
include: {
submittedBy: {
select: { id: true, name: true, email: true },
},
agency: true,
},
orderBy: { createdAt: 'asc' },
});
}
async approveAgencyUpdateRequest(moderationId: number, adminId: number) {
return this.prisma.client.$transaction(async (tx: Prisma.TransactionClient) => {
const moderationItem = await tx.moderationQueue.findUnique({
where: { id: moderationId },
});
Iif (
!moderationItem ||
moderationItem.status !== ModerationStatus.PENDING ||
moderationItem.type !== ModerationType.AGENCY_PROFILE_UPDATE
) {
throw new NotFoundException('Pending agency update request not found.');
}
Iif (!moderationItem.agencyId) {
throw new BadRequestException(
'Invalid moderation item: agencyId is missing.',
);
}
await tx.agency.update({
where: { id: moderationItem.agencyId },
data: moderationItem.moderatedContent as any,
});
return tx.moderationQueue.update({
where: { id: moderationId },
data: {
status: ModerationStatus.APPROVED,
reviewedById: adminId,
},
});
});
}
async rejectAgencyUpdateRequest(
moderationId: number,
adminId: number,
{ reason }: RejectDto,
) {
const moderationItem = await this.prisma.client.moderationQueue.findFirst({
where: {
id: moderationId,
status: ModerationStatus.PENDING,
type: ModerationType.AGENCY_PROFILE_UPDATE,
},
});
Iif (!moderationItem) {
throw new NotFoundException('Pending agency update request not found.');
}
return this.prisma.client.moderationQueue.update({
where: { id: moderationId },
data: {
status: ModerationStatus.REJECTED,
reviewedById: adminId,
reason,
},
});
}
getReportedListings() {
return this.prisma.client.moderationQueue.findMany({
where: {
type: ModerationType.LISTING_REPORT,
status: ModerationStatus.PENDING,
},
include: {
submittedBy: {
select: { id: true, name: true, email: true },
},
listingReport: {
include: {
listing: {
select: { id: true, title: true },
},
},
},
},
orderBy: { createdAt: 'asc' },
});
}
async approveListingReport(moderationId: number, adminId: number) {
return this.prisma.client.$transaction(async (tx: Prisma.TransactionClient) => {
const moderationItem = await tx.moderationQueue.findFirst({
where: {
id: moderationId,
status: ModerationStatus.PENDING,
type: ModerationType.LISTING_REPORT,
},
});
Iif (!moderationItem) {
throw new NotFoundException('Pending listing report not found.');
}
Iif (!moderationItem.listingId) {
throw new BadRequestException(
'Invalid moderation item: listingId is missing',
);
}
await tx.listing.update({
where: { id: moderationItem.listingId },
data: { isVerified: false },
});
return tx.moderationQueue.update({
where: { id: moderationId },
data: {
status: ModerationStatus.APPROVED,
reviewedById: adminId,
},
});
});
}
async rejectListingReport(
moderationId: number,
adminId: number,
{ reason }: RejectDto,
) {
const moderationItem = await this.prisma.client.moderationQueue.findFirst({
where: {
id: moderationId,
status: ModerationStatus.PENDING,
type: ModerationType.LISTING_REPORT,
},
});
Iif (!moderationItem) {
throw new NotFoundException('Pending listing report not found.');
}
return this.prisma.client.moderationQueue.update({
where: { id: moderationId },
data: {
status: ModerationStatus.REJECTED,
reviewedById: adminId,
reason,
},
});
}
// ────── Bulk moderation actions ──────────────────────────────────────
async bulkModerationAction(type: ModerationType, ids: number[], action: 'APPROVED' | 'REJECTED', adminId: number, reason?: string) {
Iif (!Array.isArray(ids) || ids.length === 0) return { updated: 0 };
const status = action === 'APPROVED' ? ModerationStatus.APPROVED : ModerationStatus.REJECTED;
const updated = await this.prisma.client.moderationQueue.updateMany({
where: { id: { in: ids }, type, status: ModerationStatus.PENDING },
data: { status, reviewedById: adminId, reason: reason || undefined },
});
return { updated: updated.count };
}
// ────── Feature Flags ────────────────────────────────────────────────
listFeatureFlags() {
return (this.prisma.client as any).featureFlag.findMany({ orderBy: { key: 'asc' } });
}
async toggleFeatureFlag(key: string) {
const existing = await (this.prisma.client as any).featureFlag.findUnique({ where: { key } });
if (!existing) {
return (this.prisma.client as any).featureFlag.create({ data: { key, enabled: true } });
}
return (this.prisma.client as any).featureFlag.update({ where: { key }, data: { enabled: !existing.enabled } });
}
} |