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

Validate actions during their creation #1354

Merged
merged 8 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -153,12 +153,7 @@
/**
* Superclass for all responses to Job changes
*/
export interface JobAction<DtoType> {

Check warning on line 156 in src/jobs/config/jobconfig.ts

View workflow job for this annotation

GitHub Actions / eslint

'DtoType' is defined but never used
/**
* 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