Skip to content

Commit

Permalink
Validate actions during their creation (#1354)
Browse files Browse the repository at this point in the history
* remove validation from actions, store only configVersion inside each job

* remove action validation from testing

* fix testing

* restore configuration in the schema, fix tests

* fix linting

* fix error message
  • Loading branch information
despadam authored Aug 7, 2024
1 parent e328413 commit b218952
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 121 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,5 @@ jobs:
run: |
cp CI/ESS/docker-compose.api.yaml docker-compose.yaml
cp functionalAccounts.json.test functionalAccounts.json
docker-compose up --build -d
docker compose up --build -d
npm run test:api
7 changes: 0 additions & 7 deletions src/jobs/actions/emailaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,6 @@ export class EmailJobAction<T> implements JobAction<T> {
this.bodyTemplate = compile(data["body"]);
}

async validate(dto: T) {
Logger.log(
"Validating EmailJobAction: " + JSON.stringify(dto),
"EmailJobAction",
);
}

async performJob(job: JobClass) {
Logger.log(
"Performing EmailJobAction: " + JSON.stringify(job),
Expand Down
4 changes: 0 additions & 4 deletions src/jobs/actions/logaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ export class LogJobAction<T> implements JobAction<T> {
return LogJobAction.actionType;
}

async validate(dto: T) {
Logger.log("Validating job: " + JSON.stringify(dto), "LogJobAction");
}

async performJob(job: JobClass) {
Logger.log("Performing job: " + JSON.stringify(job), "LogJobAction");
}
Expand Down
3 changes: 0 additions & 3 deletions src/jobs/actions/rabbitmqaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ export class RabbitMQJobAction<T> implements JobAction<T> {
return RabbitMQJobAction.actionType;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
async validate(dto: T) {}

async performJob(job: JobClass) {
Logger.log(
"Performing RabbitMQJobAction: " + JSON.stringify(job),
Expand Down
19 changes: 14 additions & 5 deletions src/jobs/actions/urlaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,9 @@ export class URLAction<T> implements JobAction<T> {
return URLAction.actionType;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
async validate(dto: T) {}

async performJob(job: JobClass) {
const url = encodeURI(this.urlTemplate(job, jobTemplateOptions));
Logger.log(`Requesting ${url}`, "URLAction");
Logger.log(`Requesting ${url}`, "UrlJobAction");

const response = await fetch(url, {
method: this.method,
Expand All @@ -74,7 +71,11 @@ export class URLAction<T> implements JobAction<T> {
: undefined,
});

Logger.log(`Request for ${url} returned ${response.status}`, "URLAction");
Logger.log(
`Request for ${url} returned ${response.status}`,
"UrlJobAction",
);

if (!response.ok) {
throw new HttpException(
{
Expand All @@ -101,13 +102,20 @@ export class URLAction<T> implements JobAction<T> {
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(data: Record<string, any>) {
Logger.log(
"Initializing UrlJobAction. Params: " + JSON.stringify(data),
"UrlJobAction",
);

if (!data["url"]) {
throw new NotFoundException("Param 'url' is undefined in url action");
}
this.urlTemplate = Handlebars.compile(data.url);

if (data["method"]) {
this.method = data.method;
}

if (data["headers"]) {
if (!isStringRecord(data.headers)) {
throw new NotFoundException(
Expand All @@ -121,6 +129,7 @@ export class URLAction<T> implements JobAction<T> {
]),
);
}

if (data["body"]) {
this.bodyTemplate = Handlebars.compile(data["body"]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/jobs/config/jobconfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ describe("Job configuration", () => {
const action = create.actions[0];
expect(action instanceof LogJobAction);
expect(action.getActionType()).toBe("log");
action.validate({ config: null });
// action.validate({ config: null });
});
});
5 changes: 0 additions & 5 deletions src/jobs/config/jobconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,6 @@ function parseAction<DtoType>(
* Superclass for all responses to Job changes
*/
export interface JobAction<DtoType> {
/**
* Validate the DTO, throwing an HttpException for problems
*/
validate: (dto: DtoType) => Promise<void>;

/**
* Respond to the action
*/
Expand Down
39 changes: 19 additions & 20 deletions src/jobs/interceptors/job-create.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,25 @@ export class JobCreateInterceptor implements NestInterceptor {
}
const jc = matchingConfig[0];

await Promise.all(
jc.create.actions.map((action) => {
return action.validate(createJobDto).catch((err) => {
Logger.error(err);
if (err instanceof HttpException) {
throw err;
}

throw new HttpException(
{
status: HttpStatus.BAD_REQUEST,
message: `Invalid job input. Action ${action.getActionType()} unable to validate ${
createJobDto.type
} job due to ${err}`,
},
HttpStatus.BAD_REQUEST,
);
});
}),
);
// await Promise.all(
// jc.create.actions.map((action) => {
// return action.validate(createJobDto).catch((err) => {
// Logger.error(err);
// if (err instanceof HttpException) {
// throw err;
// }
// throw new HttpException(
// {
// status: HttpStatus.BAD_REQUEST,
// message: `Invalid job input. Action ${action.getActionType()} unable to validate ${
// createJobDto.type
// } job due to ${err}`,
// },
// HttpStatus.BAD_REQUEST,
// );
// });
// }),
// );

return jc;
}
Expand Down
Loading

0 comments on commit b218952

Please sign in to comment.