Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 52 additions & 19 deletions apps/api/src/offboarding-checklist/access-revocation.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Logger,
NotFoundException,
} from '@nestjs/common';
import { AttachmentEntityType, db } from '@db';
import { AttachmentEntityType, Prisma, db } from '@db';
import { AttachmentsService } from '../attachments/attachments.service';

@Injectable()
Expand All @@ -30,17 +30,42 @@ export class AccessRevocationService {
revocations.map((r) => [r.vendorId, r]),
);

const revocationIds = revocations.map((r) => r.id);
const allAttachments =
revocationIds.length > 0
? await db.attachment.findMany({
where: {
organizationId,
entityId: { in: revocationIds },
entityType: AttachmentEntityType.offboarding_checklist,
},
orderBy: { createdAt: 'asc' },
})
: [];

const attachmentsByRevocation = new Map<string, typeof allAttachments>();
for (const attachment of allAttachments) {
const existing = attachmentsByRevocation.get(attachment.entityId) ?? [];
existing.push(attachment);
attachmentsByRevocation.set(attachment.entityId, existing);
}

const vendorList = await Promise.all(
vendors.map(async (vendor) => {
const revocation = revocationMap.get(vendor.id);
const domain = vendor.website?.replace(/^https?:\/\//, '').replace(/\/.*$/, '') ?? null;
const evidence = revocation
? await this.attachmentsService.getAttachments(
organizationId,
revocation.id,
AttachmentEntityType.offboarding_checklist,
)
const rawAttachments = revocation
? (attachmentsByRevocation.get(revocation.id) ?? [])
: [];
const evidence = await Promise.all(
rawAttachments.map(async (attachment) => ({
id: attachment.id,
name: attachment.name,
type: attachment.type,
downloadUrl: await this.attachmentsService.getPresignedDownloadUrl(attachment.url),
createdAt: attachment.createdAt,
})),
);
return {
vendorId: vendor.id,
vendorName: vendor.name,
Expand Down Expand Up @@ -102,18 +127,26 @@ export class AccessRevocationService {
);
}

const revocation = await db.offboardingAccessRevocation.create({
data: {
organizationId,
memberId,
vendorId,
revokedById,
notes,
},
include: {
revokedBy: { select: { id: true, name: true, email: true } },
},
});
let revocation: Awaited<ReturnType<typeof db.offboardingAccessRevocation.create>>;
try {
revocation = await db.offboardingAccessRevocation.create({
data: {
organizationId,
memberId,
vendorId,
revokedById,
notes,
},
include: {
revokedBy: { select: { id: true, name: true, email: true } },
},
});
} catch (err) {
if (err instanceof Prisma.PrismaClientKnownRequestError && err.code === 'P2002') {
throw new BadRequestException('Vendor access has already been revoked for this member');
}
throw err;
}

if (evidence) {
try {
Expand Down
14 changes: 12 additions & 2 deletions apps/api/src/offboarding-checklist/offboarding-export.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ export class OffboardingExportService {
output: NodeJS.WritableStream;
}) {
const archive = archiver('zip', { zlib: { level: 9 } });
archive.on('error', () => { archive.abort(); });
archive.on('error', (err) => {
archive.abort();
if ('destroy' in output && typeof (output as { destroy?: unknown }).destroy === 'function') {
(output as { destroy: (err: Error) => void }).destroy(err);
}
});
archive.pipe(output);

const checklist =
Expand Down Expand Up @@ -153,7 +158,12 @@ export class OffboardingExportService {
output: NodeJS.WritableStream;
}) {
const archive = archiver('zip', { zlib: { level: 9 } });
archive.on('error', () => { archive.abort(); });
archive.on('error', (err) => {
archive.abort();
if ('destroy' in output && typeof (output as { destroy?: unknown }).destroy === 'function') {
(output as { destroy: (err: Error) => void }).destroy(err);
}
});
archive.pipe(output);

const BATCH_SIZE = 50;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ export function OffboardingChecklistItem({

const handleFileDrop = async (e: React.DragEvent<HTMLDivElement>) => {
e.preventDefault();
if (isProcessing) return;
const file = e.dataTransfer.files[0];
if (!file) return;
await handleFileUpload(file);
Expand Down Expand Up @@ -373,8 +374,8 @@ function EvidenceContent({
<div
onDrop={onFileDrop}
onDragOver={(e) => e.preventDefault()}
onClick={() => dropzoneInputRef.current?.click()}
className="flex cursor-pointer flex-col items-center gap-2 rounded-md border-2 border-dashed border-muted-foreground/25 px-4 py-6 text-center transition hover:border-muted-foreground/50 hover:bg-muted/25"
onClick={() => !isProcessing && dropzoneInputRef.current?.click()}
className={`flex cursor-pointer flex-col items-center gap-2 rounded-md border-2 border-dashed border-muted-foreground/25 px-4 py-6 text-center transition hover:border-muted-foreground/50 hover:bg-muted/25${isProcessing ? ' pointer-events-none opacity-50' : ''}`}
>
<Upload size={20} className="text-muted-foreground" />
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function OffboardingSummaryCard({
completedItems,
hasEvidence,
}: OffboardingSummaryCardProps) {
const daysSince = differenceInDays(new Date(), new Date(offboardDate));
const daysSince = Math.max(0, differenceInDays(new Date(), new Date(offboardDate)));
const progressPercent =
totalItems > 0 ? Math.round((completedItems / totalItems) * 100) : 0;

Expand Down
Loading