Skip to content

Commit

Permalink
Toteutettu pollaaminen dokumenttiprosessille
Browse files Browse the repository at this point in the history
  • Loading branch information
pretseli committed Jan 21, 2025
1 parent ac08e8b commit 1a97846
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ const FileSelector = forwardRef<FileSelectorRef, FileSelectorProps>(
},
);

export const ExcelUploadButton = ({
export const PistesyottoExcelUploadButton = ({
hakuOid,
hakukohdeOid,
}: {
Expand Down
50 changes: 38 additions & 12 deletions src/app/lib/valintalaskentakoostepalvelu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { HarkinnanvaraisuudenSyy } from './types/harkinnanvaraiset-types';
import { ValintakoeAvaimet } from './types/valintaperusteet-types';
import { Hakukohde } from './types/kouta-types';
import { getOpetuskieliCode } from './kouta';
import { AssertionError } from 'assert';

export const getHakukohteenValintatuloksetIlmanHakijanTilaa = async (
hakuOid: string,
Expand Down Expand Up @@ -259,20 +260,44 @@ export type GetValintakoeExcelParams = {
valintakoeTunniste: Array<string>;
};

const pollDocumentProcess = async (processId: string) => {
let pollTimes = 10;

while (pollTimes) {
const processRes = await client.get<{
dokumenttiId: string;
kasittelyssa: boolean;
keskeytetty: boolean;
kokonaistyo: {
valmis: boolean;
};
poikkeukset: Array<{
viesti: string;
}>;
}>(configuration.dokumenttiProsessiUrl({ id: processId }));
pollTimes -= 1;

const { data } = processRes;

if (data.kokonaistyo?.valmis || data.keskeytetty) {
return data;
} else if (pollTimes === 0) {
throw new OphApiError(
processRes,
'Dokumentin prosessointi aikakatkaistiin',
);
}
await new Promise((resolve) => setTimeout(resolve, 2000));
}
throw new AssertionError({
message: 'Dokumentin prosessoinnin pollaus päättyi ilman tulosta!',
});
};

const downloadProcessDocument = async (processId: string) => {
const processRes = await client.get<{
dokumenttiId: string;
kasittelyssa: boolean;
keskeytetty: boolean;
kokonaistyo: {
valmis: boolean;
};
poikkeukset: Array<{
viesti: string;
}>;
}>(configuration.dokumenttiProsessiUrl({ id: processId }));
const data = await pollDocumentProcess(processId);

const { dokumenttiId, poikkeukset } = processRes.data;
const { dokumenttiId, poikkeukset } = data;

if (!isEmpty(poikkeukset)) {
const errorMessages = poikkeukset.map(prop('viesti')).join('\n');
Expand All @@ -282,6 +307,7 @@ const downloadProcessDocument = async (processId: string) => {
const documentRes = await client.get<Blob>(
configuration.lataaDokumenttiUrl({ dokumenttiId }),
);

return createFileResult(documentRes);
};

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/pistesyotto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ test.describe('Excel export', () => {
),
async (route) => {
await route.fulfill({
json: { dokumenttiId: 'doc_id' },
json: { dokumenttiId: 'doc_id', kokonaistyo: { valmis: true } },
});
},
);
Expand Down

0 comments on commit 1a97846

Please sign in to comment.