Skip to content

Commit

Permalink
feat: add QoL property to list available versions
Browse files Browse the repository at this point in the history
  • Loading branch information
niekcandaele committed Dec 15, 2024
1 parent ed7415d commit 4a3bdad
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 10 deletions.
16 changes: 15 additions & 1 deletion packages/app-api/src/service/Module/dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TakaroDTO, TakaroModelDTO } from '@takaro/util';
import { Type } from 'class-transformer';
import { IsString, IsJSON, IsOptional, ValidateNested, IsUUID, IsObject, Length } from 'class-validator';
import { IsString, IsJSON, IsOptional, ValidateNested, IsUUID, IsObject, Length, IsISO8601 } from 'class-validator';
import { CommandCreateDTO, CommandOutputDTO } from '../CommandService.js';
import { CronJobCreateDTO, CronJobOutputDTO } from '../CronJobService.js';
import { FunctionCreateDTO, FunctionOutputDTO } from '../FunctionService.js';
Expand Down Expand Up @@ -37,6 +37,17 @@ export class ModuleVersionOutputDTO extends TakaroModelDTO<ModuleVersionOutputDT
permissions: PermissionOutputDTO[];
}

export class SmallModuleVersionOutputDTO extends TakaroDTO<SmallModuleVersionOutputDTO> {
@IsUUID('4')
id: string;
@IsString()
tag: string;
@IsISO8601()
createdAt: string;
@IsISO8601()
updatedAt: string;
}

export class ModuleOutputDTO extends TakaroModelDTO<ModuleOutputDTO> {
@IsString()
name: string;
Expand All @@ -46,6 +57,9 @@ export class ModuleOutputDTO extends TakaroModelDTO<ModuleOutputDTO> {
@ValidateNested()
@Type(() => ModuleVersionOutputDTO)
latestVersion: ModuleVersionOutputDTO;
@ValidateNested({ each: true })
@Type(() => SmallModuleVersionOutputDTO)
versions: SmallModuleVersionOutputDTO[];
}

export class ModuleInstallationOutputDTO extends TakaroModelDTO<ModuleInstallationOutputDTO> {
Expand Down
27 changes: 23 additions & 4 deletions packages/app-api/src/service/Module/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ModuleModel, ModuleRepo } from '../../db/module.js';
import { CronJobCreateDTO, CronJobService } from '../CronJobService.js';
import { HookCreateDTO, HookService } from '../HookService.js';
import { errors, traceableClass } from '@takaro/util';
import { ITakaroQuery } from '@takaro/db';
import { ITakaroQuery, SortDirection } from '@takaro/db';
import { PaginatedOutput } from '../../db/base.js';
import { CommandCreateDTO, CommandService } from '../CommandService.js';
import {
Expand Down Expand Up @@ -34,6 +34,7 @@ import {
ModuleUpdateDTO,
ModuleVersionOutputDTO,
ModuleVersionUpdateDTO,
SmallModuleVersionOutputDTO,
} from './dto.js';

const Ajv = _Ajv as unknown as typeof _Ajv.default;
Expand All @@ -55,9 +56,27 @@ export class ModuleService extends TakaroService<ModuleModel, ModuleOutputDTO, M
return new ModuleRepo(this.domainId);
}

private async addLatestVersion(mod: ModuleOutputDTO) {
private async extendModuleDTO(mod: ModuleOutputDTO): Promise<ModuleOutputDTO> {
const latestVersion = await this.getLatestVersion(mod.id);
mod.latestVersion = latestVersion;

mod.versions = (
await this.repo.findVersions({
filters: {
moduleId: [mod.id],
},
sortBy: 'updatedAt',
sortDirection: SortDirection.desc,
})
).results.map(
(v) =>
new SmallModuleVersionOutputDTO({
createdAt: v.createdAt,
updatedAt: v.updatedAt,
id: v.id,
tag: v.tag,
}),
);
return mod;
}

Expand All @@ -66,7 +85,7 @@ export class ModuleService extends TakaroService<ModuleModel, ModuleOutputDTO, M

return {
total: results.total,
results: await Promise.all(results.results.map((m) => this.addLatestVersion(m))),
results: await Promise.all(results.results.map((m) => this.extendModuleDTO(m))),
};
}

Expand All @@ -82,7 +101,7 @@ export class ModuleService extends TakaroService<ModuleModel, ModuleOutputDTO, M

async findOne(id: string): Promise<ModuleOutputDTO | undefined> {
const result = await this.repo.findOne(id);
return result ? this.addLatestVersion(result) : undefined;
return result ? this.extendModuleDTO(result) : undefined;
}

async findOneVersion(id: string): Promise<ModuleVersionOutputDTO | undefined> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@
],
"functions": [],
"systemConfigSchema": "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"type\":\"object\",\"properties\":{\"enabled\":{\"type\":\"boolean\",\"default\":true,\"description\":\"Enable/disable the module without having to uninstall it.\"}},\"required\":[],\"additionalProperties\":false}"
}
},
"versions": [
{
"createdAt": "2024-12-15T21:33:13.430Z",
"id": "f33721ae-4955-4664-9979-0c695ad43617",
"tag": "latest"
}
]
}
},
"status": 200,
Expand Down
9 changes: 8 additions & 1 deletion packages/test/src/__snapshots__/ModuleController/Create.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@
"permissions": [],
"functions": [],
"systemConfigSchema": "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"type\":\"object\",\"properties\":{\"enabled\":{\"type\":\"boolean\",\"default\":true,\"description\":\"Enable/disable the module without having to uninstall it.\"}},\"required\":[],\"additionalProperties\":false}"
}
},
"versions": [
{
"createdAt": "2024-12-15T21:33:09.124Z",
"id": "69f92eb1-6859-4d05-a33d-52f5454bcd68",
"tag": "latest"
}
]
}
},
"status": 200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@
"permissions": [],
"functions": [],
"systemConfigSchema": "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"type\":\"object\",\"properties\":{\"enabled\":{\"type\":\"boolean\",\"default\":true,\"description\":\"Enable/disable the module without having to uninstall it.\"}},\"required\":[],\"additionalProperties\":false}"
}
},
"versions": [
{
"createdAt": "2024-12-15T21:33:08.332Z",
"id": "1bd638d3-c2a2-4ac5-b2b0-3f0b74631acf",
"tag": "latest"
}
]
}
},
"status": 200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@
],
"functions": [],
"systemConfigSchema": "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"type\":\"object\",\"properties\":{\"enabled\":{\"type\":\"boolean\",\"default\":true,\"description\":\"Enable/disable the module without having to uninstall it.\"}},\"required\":[],\"additionalProperties\":false}"
}
},
"versions": [
{
"createdAt": "2024-12-15T21:33:16.264Z",
"id": "eb9a782c-fa98-4452-9deb-308693eaef2c",
"tag": "latest"
}
]
}
},
"status": 200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@
"permissions": [],
"functions": [],
"systemConfigSchema": "{\"$schema\":\"http://json-schema.org/draft-07/schema#\",\"type\":\"object\",\"properties\":{\"enabled\":{\"type\":\"boolean\",\"default\":true,\"description\":\"Enable/disable the module without having to uninstall it.\"}},\"required\":[],\"additionalProperties\":false}"
}
},
"versions": [
{
"createdAt": "2024-12-15T21:33:15.532Z",
"id": "1311313c-542d-4ccc-b7fe-dc9f00c9e8c6",
"tag": "latest"
}
]
}
},
"status": 200,
Expand Down

0 comments on commit 4a3bdad

Please sign in to comment.