All files / components/admin AdminInvestorAgreementsSection.tsx

65.62% Statements 21/32
70% Branches 14/20
45.45% Functions 5/11
67.74% Lines 21/31

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                                                                              2x 2x 1x   1x 1x                 3x 3x       9x 9x 9x   9x 6x   12x         3x       3x   9x 1x             2x                     2x       2x 2x                                                                                                    
// src/components/admin/AdminInvestorAgreementsSection.tsx
//
// Read-only view of a single investor's outbound PandaDoc agreements
// (investment + loan documents) for the admin investor detail page (FSC-57).
// Mirrors the investor-facing AgreementsTab display, minus the sign/resend
// controls — admins observe progress and can pull the signed PDF.
import {type ReactElement, useState} from 'react';
import {Alert, Box, Button, Chip, Paper, Skeleton, Snackbar, Stack, Typography} from '@mui/material';
import {
	CheckCircle as CompletedIcon,
	Download as DownloadIcon,
	HourglassEmpty as PendingIcon,
	WarningAmber as WarningIcon
} from '@mui/icons-material';
import {
	ACTION_REQUIRED_STATUSES,
	COMPLETED_STATUSES,
	DOCUMENT_STATUS_LABELS,
	type DocumentStatus,
	INACTIVE_STATUSES,
	type PandaDocDocument
} from '../../services/documentsApi';
import {
	useDownloadInvestorAgreementMutation,
	useGetInvestorAgreementsQuery
} from '../../services/adminDocumentsApi';
import {downloadBlob} from '../../utils/downloadBlob';
 
interface SnackbarState {
	open: boolean;
	message: string;
	severity: 'success' | 'error';
}
 
interface AdminInvestorAgreementsSectionProps {
	investorId: number;
}
 
function StatusChip({status}: {status: DocumentStatus}): ReactElement {
	const label = DOCUMENT_STATUS_LABELS[status];
	if (COMPLETED_STATUSES.includes(status)) {
		return <Chip label={label} color="success" size="small" icon={<CompletedIcon/>}/>;
	}
	Eif (ACTION_REQUIRED_STATUSES.includes(status)) {
		return <Chip label={label} color="warning" size="small" icon={<PendingIcon/>}/>;
	}
	if (INACTIVE_STATUSES.includes(status)) {
		return <Chip label={label} color="default" size="small" icon={<WarningIcon/>}/>;
	}
	return <Chip label={label} color="default" size="small"/>;
}
 
function formatDate(dateStr: string | null): string {
	Iif (dateStr === null) return '—';
	return new Date(dateStr).toLocaleDateString('en-US', {year: 'numeric', month: 'short', day: 'numeric'});
}
 
export default function AdminInvestorAgreementsSection({investorId}: AdminInvestorAgreementsSectionProps): ReactElement {
	const {data, isLoading, isError} = useGetInvestorAgreementsQuery(investorId);
	const [downloadAgreement, {isLoading: isDownloading}] = useDownloadInvestorAgreementMutation();
	const [snackbar, setSnackbar] = useState<SnackbarState>({open: false, message: '', severity: 'success'});
 
	if (isLoading) {
		return (
			<Stack spacing={1.5}>
				{[1, 2].map((i) => <Skeleton key={i} variant="rounded" height={72}/>)}
			</Stack>
		);
	}
 
	Iif (isError) {
		return <Alert severity="error">Unable to load this investor's agreements.</Alert>;
	}
 
	const documents = data?.documents ?? [];
 
	if (documents.length === 0) {
		return (
			<Typography variant="body2" sx={{color: 'text.secondary'}}>
				No agreements have been sent to this investor yet.
			</Typography>
		);
	}
 
	const handleDownload = (doc: PandaDocDocument): void => {
		downloadAgreement({investorId, documentId: doc.id})
			.unwrap()
			.then((blob) => {
				downloadBlob(blob, `${doc.name}.pdf`);
			})
			.catch(() => {
				setSnackbar({open: true, message: 'Failed to download agreement.', severity: 'error'});
			});
	};
 
	return (
		<Box>
			<Stack spacing={1.5}>
				{documents.map((doc) => {
					const canDownload = COMPLETED_STATUSES.includes(doc.status);
					return (
						<Paper key={doc.id} variant="outlined" sx={{p: 2, borderRadius: 2}}>
							<Stack direction={{xs: 'column', sm: 'row'}} spacing={2} sx={{alignItems: {sm: 'center'}}}>
								<Box sx={{flexGrow: 1, minWidth: 0}}>
									<Stack direction="row" spacing={1} sx={{alignItems: 'center', flexWrap: 'wrap', mb: 0.5}}>
										<Typography variant="body1" sx={{fontWeight: 600}}>{doc.name}</Typography>
										<StatusChip status={doc.status}/>
									</Stack>
									<Stack direction="row" spacing={3} sx={{flexWrap: 'wrap', color: 'text.secondary'}}>
										<Typography variant="caption">Sent: {formatDate(doc.dateSent)}</Typography>
										{doc.dateCompleted !== null && (
											<Typography variant="caption">Completed: {formatDate(doc.dateCompleted)}</Typography>
										)}
									</Stack>
								</Box>
								{canDownload && (
									<Button
										variant="outlined"
										size="small"
										startIcon={<DownloadIcon/>}
										disabled={isDownloading}
										onClick={() => {
											handleDownload(doc);
										}}
									>
										Download
									</Button>
								)}
							</Stack>
						</Paper>
					);
				})}
			</Stack>
 
			<Snackbar
				open={snackbar.open}
				autoHideDuration={6000}
				onClose={() => setSnackbar({...snackbar, open: false})}
			>
				<Alert
					onClose={() => setSnackbar({...snackbar, open: false})}
					severity={snackbar.severity}
					sx={{width: '100%'}}
				>
					{snackbar.message}
				</Alert>
			</Snackbar>
		</Box>
	);
}