Skip to content

Commit f2ddffa

Browse files
authored
Merge pull request #1470 from navikt/feature/forms-api-bulk-publishing
Feature/forms api bulk publishing
2 parents e6439a1 + 71b4269 commit f2ddffa

27 files changed

+187
-715
lines changed

packages/bygger-backend/src/Backend.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,16 @@ import {
1313
performChangesOnSeparateBranch,
1414
pushFilesAndUpdateMonorepoRefCallback,
1515
} from './repoUtils.js';
16-
import { FormioService } from './services/formioService';
1716

1817
const BULK_PUBLISH_REGEXP = /^\[bulk-publisering\] (\d+) skjemaer publisert, monorepo ref: (.*)$/;
1918
const PUBLISH_REGEXP = /^\[publisering\] skjema "(.*)", monorepo ref: (.*)$/;
2019
const UNPUBLISH_REGEXP = /^\[avpublisering\] skjema (.*), monorepo ref: (.*)$/;
2120

2221
export class Backend {
2322
private readonly config: ConfigType;
24-
private readonly formioService: FormioService;
2523

26-
constructor(config: ConfigType, formioService: FormioService) {
24+
constructor(config: ConfigType) {
2725
this.config = config;
28-
this.formioService = formioService;
2926
}
3027

3128
private async createGitHubRepo() {
@@ -127,11 +124,6 @@ export class Backend {
127124
);
128125
}
129126

130-
async bulkPublishForms(formPaths: string[]) {
131-
const forms: any = await this.formioService.getForms(formPaths);
132-
return this.publishForms(forms);
133-
}
134-
135127
async publishForms(forms: NavFormType[]) {
136128
const skjemautfyllingRepo = await this.createGitHubRepo();
137129
const branchName = `bulkpublish--${uuidv4()}`;

packages/bygger-backend/src/fetchUtils.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { logger } from './logging/logger';
33

44
export class HttpError extends Error {
55
readonly response: Response;
6+
readonly bodyErrorMessage?: string;
67

7-
constructor(fetchResponse: Response) {
8+
constructor(fetchResponse: Response, bodyErrorMessage?: string) {
89
super(`${fetchResponse.status} ${fetchResponse.statusText} fetching: ${fetchResponse.url}`);
910
this.name = this.constructor.name;
1011
this.response = fetchResponse;
12+
this.bodyErrorMessage = bodyErrorMessage;
1113
}
1214
}
1315

packages/bygger-backend/src/routers/api/deprecated-publish-bulk.ts

-16
This file was deleted.

packages/bygger-backend/src/routers/api/deprecated-publish-form.ts

-14
This file was deleted.

packages/bygger-backend/src/routers/api/deprecated-unpublish-form.ts

-14
This file was deleted.

packages/bygger-backend/src/routers/api/form-publications/form-publications.ts

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { formioFormsApiUtils, TranslationLang } from '@navikt/skjemadigitalisering-shared-domain';
22
import { RequestHandler } from 'express';
3+
import { logger } from '../../../logging/logger';
34
import { formPublicationsService, formsService } from '../../../services';
5+
import { FormPublication } from '../../../services/formPublications/types';
6+
import { BadRequest } from '../helpers/errors';
47
import { mapLanguageCodeToFormioFormat } from './utils';
58

69
const getAll: RequestHandler = async (req, res, next) => {
@@ -28,12 +31,14 @@ const post: RequestHandler = async (req, res, next) => {
2831
const accessToken = req.headers.AzureAccessToken as string;
2932

3033
try {
34+
logger.info(`Publishing form ${formPath}...`);
3135
const form = await formPublicationsService.post(
3236
formPath,
3337
languageCodes as TranslationLang[],
3438
parseInt(revision as string),
3539
accessToken,
3640
);
41+
logger.info(`Form ${formPath} published successfully`);
3742
const { translations } = await formPublicationsService.getTranslations(
3843
formPath,
3944
languageCodes as TranslationLang[],
@@ -53,6 +58,36 @@ const post: RequestHandler = async (req, res, next) => {
5358
}
5459
};
5560

61+
const postBulk: RequestHandler = async (req, res, next) => {
62+
const { formPaths } = req.body.payload;
63+
const accessToken = req.headers.AzureAccessToken as string;
64+
65+
if (!Array.isArray(formPaths) || formPaths.length === 0) {
66+
next(new BadRequest('Request is missing formPaths'));
67+
return;
68+
}
69+
70+
try {
71+
const allForms = await formsService.getAll('path,revision');
72+
const formPublications: FormPublication[] = allForms
73+
.filter((form) => formPaths.includes(form.path))
74+
.map((form) => ({ path: form.path, revision: form.revision! }));
75+
logger.info(`Bulk-publishing ${formPublications.length}...`, { formPaths });
76+
77+
const bulkPublicationResult = await formPublicationsService.postAll(formPublications, accessToken);
78+
const successCounter = bulkPublicationResult.filter((r) => r.status === 'ok');
79+
const failureCounter = bulkPublicationResult.filter((r) => r.status !== 'ok');
80+
logger.info(`Published ${successCounter.length} forms (failed: ${failureCounter.length})`, {
81+
bulkPublicationResult,
82+
});
83+
84+
req.body = { bulkPublicationResult };
85+
next();
86+
} catch (error) {
87+
next(error);
88+
}
89+
};
90+
5691
const unpublish: RequestHandler = async (req, _res, next) => {
5792
try {
5893
const { formPath } = req.params;
@@ -77,6 +112,6 @@ const getTranslations = async (req, res, next) => {
77112
}
78113
};
79114

80-
const formPublications = { getAll, get, post, delete: unpublish, getTranslations };
115+
const formPublications = { getAll, get, post, postBulk, delete: unpublish, getTranslations };
81116

82117
export default formPublications;

packages/bygger-backend/src/routers/api/form-publications/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import express from 'express';
22
import authHandlers from '../helpers/authHandlers';
33
import publishForm from '../publish-form';
4+
import publishForms from '../publish-forms';
45
import unpublishForm from '../unpublish-form';
56
import formPublications from './form-publications';
67

78
const formPublicationsRouter = express.Router();
89
const { formsApiAuthHandler } = authHandlers;
910

1011
formPublicationsRouter.get('/', formPublications.getAll);
12+
formPublicationsRouter.post('/', formsApiAuthHandler, formPublications.postBulk, publishForms);
1113
formPublicationsRouter.get('/:formPath', formPublications.get);
1214
formPublicationsRouter.post('/:formPath', formsApiAuthHandler, formPublications.post, publishForm);
1315
formPublicationsRouter.delete('/:formPath', formsApiAuthHandler, formPublications.delete, unpublishForm);

packages/bygger-backend/src/routers/api/helpers/authHandlers.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import appConfig from '../../../config';
2-
import authorizedPublisher from './authorizedPublisher';
32
import azureOnBehalfOfTokenHandler from './azureOnBehalfOfTokenHandler';
43

54
const { naisClusterName, formsApi } = appConfig;
65

76
const authHandlers = {
8-
authorizedPublisher,
97
formsApiAuthHandler: azureOnBehalfOfTokenHandler(`${naisClusterName}.fyllut-sendinn.forms-api`, formsApi.devToken),
108
};
119

packages/bygger-backend/src/routers/api/helpers/authorizedPublisher.test.ts

-94
This file was deleted.

packages/bygger-backend/src/routers/api/helpers/authorizedPublisher.ts

-28
This file was deleted.

packages/bygger-backend/src/routers/api/index.ts

+1-15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import express from 'express';
22
import { rateLimiter } from '../../middleware/ratelimit';
33
import config from './config';
4-
import deprecatedPublishBulk from './deprecated-publish-bulk';
5-
import deprecatedPublishForm from './deprecated-publish-form';
6-
import deprecatedUnpublishForm from './deprecated-unpublish-form';
74
import enhetsliste from './enhetsliste';
85
import formPublicationsRouter from './form-publications';
96
import formDiff from './formDiff';
@@ -16,27 +13,16 @@ import log from './log';
1613
import migrate from './migrate';
1714
import migratePreview from './migrate-preview';
1815
import migrateUpdate from './migrate-update';
19-
import publishForm from './publish-form';
20-
import publishForms from './publish-forms';
21-
import publishResource from './publish-resource';
2216
import publishedForms from './published-forms';
2317
import recipientsRouter from './recipients';
2418
import reportsRouter from './reports';
2519
import temakoder from './temakoder';
26-
import unpublishForm from './unpublish-form';
2720

2821
const apiRouter = express.Router();
29-
const { authorizedPublisher, formsApiAuthHandler } = authHandlers;
22+
const { formsApiAuthHandler } = authHandlers;
3023

3124
apiRouter.get('/config', config);
32-
apiRouter.put('/publish/:formPath', authorizedPublisher, deprecatedPublishForm);
33-
apiRouter.delete('/publish/:formPath', authorizedPublisher, deprecatedUnpublishForm);
34-
apiRouter.put('/published-forms/:formPath', authorizedPublisher, publishForm);
3525
apiRouter.get('/published-forms/:formPath', publishedForms.get);
36-
apiRouter.delete('/published-forms/:formPath', authorizedPublisher, unpublishForm);
37-
apiRouter.post('/published-forms', authorizedPublisher, publishForms);
38-
apiRouter.post('/publish-bulk', authorizedPublisher, deprecatedPublishBulk);
39-
apiRouter.put('/published-resource/:resourceName', authorizedPublisher, publishResource);
4026
apiRouter.get('/enhetsliste', enhetsliste);
4127
apiRouter.get('/temakoder', temakoder);
4228
apiRouter.use('/reports', reportsRouter);

0 commit comments

Comments
 (0)