-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): include async job status in all relevant endpoints
- Loading branch information
Showing
14 changed files
with
169 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum AsyncJobType { | ||
Project = 'project', | ||
Scenario = 'scenario', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { | ||
ApiHideProperty, | ||
ApiProperty, | ||
ApiPropertyOptional, | ||
} from '@nestjs/swagger'; | ||
import { Exclude, plainToClass } from 'class-transformer'; | ||
import { AsyncJobType } from './async-job-type'; | ||
|
||
export class AsyncJobDto { | ||
@ApiProperty({ | ||
enum: AsyncJobType, | ||
}) | ||
type!: AsyncJobType; | ||
|
||
@ApiPropertyOptional({ | ||
isArray: true, | ||
type: String, | ||
}) | ||
ids?: string[]; | ||
|
||
@ApiProperty() | ||
started!: boolean; | ||
|
||
@ApiProperty() | ||
isoDate!: string; | ||
|
||
static forScenario(): AsyncJobDto { | ||
return plainToClass(AsyncJobDto, { | ||
type: AsyncJobType.Scenario, | ||
isoDate: new Date().toISOString(), | ||
started: true, | ||
}); | ||
} | ||
|
||
static forProject(ids?: string[]): AsyncJobDto { | ||
return plainToClass(AsyncJobDto, { | ||
type: AsyncJobType.Project, | ||
started: true, | ||
isoDate: new Date().toISOString(), | ||
ids, | ||
}); | ||
} | ||
|
||
asJsonApiMetadata(): JsonApiAsyncJobMeta { | ||
return plainToClass(JsonApiAsyncJobMeta, { | ||
meta: plainToClass(AsyncJobDto, { | ||
started: this.started, | ||
ids: this.ids, | ||
isoDate: this.isoDate, | ||
type: this.type, | ||
}), | ||
}); | ||
} | ||
} | ||
|
||
export class JsonApiAsyncJobMeta { | ||
@ApiProperty({ | ||
type: AsyncJobDto, | ||
}) | ||
meta!: AsyncJobDto; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 0 additions & 8 deletions
8
api/apps/api/src/modules/projects/dto/project-grid-request.dto.ts
This file was deleted.
Oops, something went wrong.
33 changes: 29 additions & 4 deletions
33
api/apps/api/src/modules/projects/dto/project.serializer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,45 @@ | ||
import { Injectable, NotFoundException } from '@nestjs/common'; | ||
import { PaginationMeta } from '@marxan-api/utils/app-base.service'; | ||
import { ProjectsCrudService } from '../projects-crud.service'; | ||
import { Project } from '../project.api.entity'; | ||
import { | ||
Project, | ||
ProjectResultPlural, | ||
ProjectResultSingular, | ||
} from '../project.api.entity'; | ||
import { isDefined } from '@marxan/utils'; | ||
import { AsyncJobDto } from '@marxan-api/dto/async-job.dto'; | ||
import { plainToClass } from 'class-transformer'; | ||
|
||
@Injectable() | ||
export class ProjectSerializer { | ||
constructor(private readonly projectsCrud: ProjectsCrudService) {} | ||
|
||
async serialize( | ||
entities: Partial<Project> | (Partial<Project> | undefined)[] | undefined, | ||
entities: Partial<Project> | undefined, | ||
paginationMeta?: PaginationMeta, | ||
): Promise<any> { | ||
asyncJobTriggered?: boolean, | ||
): Promise<ProjectResultSingular> { | ||
if (!isDefined(entities)) { | ||
throw new NotFoundException(); | ||
} | ||
return this.projectsCrud.serialize(entities, paginationMeta); | ||
// plainToClass produces CallStackExceeded errors? | ||
const result = await this.projectsCrud.serialize(entities, paginationMeta); | ||
return { | ||
...result, | ||
meta: { | ||
...result.meta, | ||
...(asyncJobTriggered ? AsyncJobDto.forProject() : {}), | ||
}, | ||
}; | ||
} | ||
|
||
async serializeAll( | ||
entities: (Partial<Project> | undefined)[] | undefined, | ||
paginationMeta?: PaginationMeta, | ||
) { | ||
if (!isDefined(entities)) { | ||
throw new NotFoundException(); | ||
} | ||
return await this.projectsCrud.serialize(entities, paginationMeta); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.