Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update data when applicant uploads template 9 #3846

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
54 changes: 54 additions & 0 deletions app/backend/lib/excel_import/template_nine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,60 @@ templateNine.get(
}
);

templateNine.post(
'/api/template-nine/rfi/applicant/:id/:rfiNumber',
limiter,
async (req, res) => {
const authRole = getAuthRole(req);
const pgRole = authRole?.pgRole;
const isRoleAuthorized = pgRole === 'ccbc_auth_user';
if (!isRoleAuthorized) {
return res.status(404).end();
}

const { id, rfiNumber } = req.params;

const applicationId = parseInt(id, 10);

if (!id || !rfiNumber || Number.isNaN(applicationId)) {
return res.status(400).json({ error: 'Invalid parameters' });
}
const errorList = [];
const form = formidable(commonFormidableConfig);

let files;
try {
files = await parseForm(form, req);
} catch (err) {
errorList.push({ level: 'file', error: err });
return res.status(400).json(errorList).end();
}
const filename = Object.keys(files)[0];
const uploadedFilesArray = files[filename] as Array<File>;
const uploaded = uploadedFilesArray?.[0];

if (!uploaded) {
return res.status(400).end();
}
const buf = fs.readFileSync(uploaded.filepath);
const wb = XLSX.read(buf);
let templateNineData;
try {
templateNineData = await loadTemplateNineData(wb);
} catch (err) {
errorList.push({ level: 'file', error: err });
return res.status(400).json(errorList).end();
}

if (templateNineData) {
return res.status(200).json(templateNineData);
}
return res
.status(400)
.json({ error: 'Unknown error while parsing template nine' });
}
);

templateNine.post(
'/api/template-nine/rfi/:id/:rfiNumber',
limiter,
Expand Down
61 changes: 44 additions & 17 deletions app/lib/theme/widgets/FileWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ const FileWidget: React.FC<FileWidgetProps> = ({
const { setTemplateData, rfiNumber } = formContext;
const { showToast, hideToast } = useToast();

const isApplicantPage = router.pathname.includes('applicant');

useEffect(() => {
if (rawErrors?.length > 0) {
setErrors([{ error: 'rjsf_validation' }]);
Expand Down Expand Up @@ -109,25 +111,50 @@ const FileWidget: React.FC<FileWidgetProps> = ({
});
}
} else if (templateNumber === 9) {
const response = await fetch(
`/api/template-nine/rfi/${formId}/${rfiNumber}`,
{
method: 'POST',
body: fileFormData,
if (isApplicantPage) {
// fetch for applicant and handle as expected
const response = await fetch(
`/api/template-nine/rfi/applicant/${formId}/${rfiNumber}`,
{
method: 'POST',
body: fileFormData,
}
);
if (response.ok) {
const data = await response.json();
setTemplateData({
templateNumber,
data,
templateName: file.name,
});
} else {
isTemplateValid = false;
setTemplateData({
templateNumber,
error: true,
});
}
);
if (response.ok) {
await response.json();
setTemplateData({
templateNumber,
templateName: file.name,
});
} else {
isTemplateValid = false;
setTemplateData({
templateNumber,
error: true,
});
const response = await fetch(
`/api/template-nine/rfi/${formId}/${rfiNumber}`,
{
method: 'POST',
body: fileFormData,
}
);
if (response.ok) {
await response.json();
setTemplateData({
templateNumber,
templateName: file.name,
});
} else {
isTemplateValid = false;
setTemplateData({
templateNumber,
error: true,
});
}
}
}
} catch (error) {
Expand Down
Loading
Loading