Skip to content

Commit

Permalink
feat(api): supports ids filter for GET /api/skills
Browse files Browse the repository at this point in the history
  • Loading branch information
nlepage committed Dec 24, 2024
1 parent e3ccce8 commit 79adce8
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
8 changes: 5 additions & 3 deletions api/lib/application/skills/skills.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import {
} from '../../infrastructure/repositories/index.js';
import * as updatedRecordNotifier from '../../infrastructure/event-notifier/updated-record-notifier.js';
import { generateNewId } from '../../infrastructure/utils/id-generator.js';
import { cloneSkill } from '../../domain/usecases/index.js';
import { cloneSkill, listSkills } from '../../domain/usecases/index.js';
import * as pixApiClient from '../../infrastructure/pix-api-client.js';
import { logger } from '../../infrastructure/logger.js';
import { skillSerializer } from '../../infrastructure/serializers/jsonapi/index.js';
import { extractParameters } from '../../infrastructure/utils/query-params-utils.js';

export async function clone(request, h) {
try {
Expand All @@ -36,9 +37,10 @@ export async function clone(request, h) {
}
}

export async function list() {
export async function list(req) {
try {
const skills = await skillRepository.list();
const params = extractParameters(req.query);
const skills = await listSkills(params);
return skillSerializer.serialize(skills);
} catch (err) {
logger.error(err);
Expand Down
1 change: 1 addition & 0 deletions api/lib/domain/usecases/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export * from './get-phrase-translations-url.js';
export * from './get-competence-challenges-production-overview.js';
export * from './get-competence-challenges-workbench-overview.js';
export * from './import-translations.js';
export * from './list-skills.js';
export * from './modify-localized-challenge.js';
export * from './preview-challenge.js';
export * from './proxy-delete-request-to-airtable.js';
Expand Down
8 changes: 8 additions & 0 deletions api/lib/domain/usecases/list-skills.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { skillRepository } from '../../infrastructure/repositories/index.js';

export async function listSkills({ filter }) {
if (filter.ids) {
return skillRepository.getManyByAirtableIds(filter.ids);
}
return skillRepository.list();
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ export const skillDatasource = datasource.extend({
return body;
},

async getManyByAirtableIds(ids) {
const airtableRawObjects = await findRecords(this.tableName, {
filterByFormula: `OR(${ids.map((id) => `RECORD_ID() = ${stringValue(id)}`).join(', ')})`,
});
if (airtableRawObjects.length === 0) return undefined;
return airtableRawObjects.map(this.fromAirTableObject);
},

async filterByTubeId(tubeId) {
const airtableRawObjects = await findRecords(this.tableName, {
filterByFormula: `{Tube (id persistant)} = ${stringValue(tubeId)}`,
Expand Down
7 changes: 7 additions & 0 deletions api/lib/infrastructure/repositories/skill-repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ export async function get(id) {
return toDomain(skillDTO, translations);
}

export async function getManyByAirtableIds(ids) {
const datasourceSkills = await skillDatasource.getManyByAirtableIds(ids);
if (!datasourceSkills) return [];
const translations = await translationRepository.listByEntities(model, datasourceSkills.map(({ id }) => id));
return toDomainList(datasourceSkills, translations);
}

export async function listByTubeId(tubeId) {
const datasourceSkills = await skillDatasource.filterByTubeId(tubeId);
const translations = await translationRepository.listByEntities(model, datasourceSkills.map(({ id }) => id));
Expand Down

0 comments on commit 79adce8

Please sign in to comment.