Skip to content

Commit

Permalink
Merge pull request #70 from whdalsrnt/master
Browse files Browse the repository at this point in the history
refactor: change project info in autocomplete API
  • Loading branch information
whdalsrnt authored Dec 23, 2023
2 parents f46b9ad + abc1f53 commit c2ea0f9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/controllers/add-ons/autocomplete/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
"identity.Project": {
"request": {
"search": ["project_id", "name"],
"only": ["project_id", "name", "project_group_info"]
"only": ["project_id", "name", "project_group_id"]
},
"response": {
"key": "project_id",
"name": "<%- project_group_info.name %> > <%- name %>"
"name": "<%- name %>"
}
},
"identity.ProjectGroup": {
Expand Down
40 changes: 37 additions & 3 deletions src/controllers/add-ons/autocomplete/resource.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import redisClient from '@lib/redis';
import ejs from 'ejs';
import _ from 'lodash';

Expand Down Expand Up @@ -89,13 +90,45 @@ interface AutocompleteResource {
data?: Record<string, string>;
}

const makeResponse = (params, resources) => {
const getProjectGroupMap = async () => {
const client = await getClient('identity');
const response = await client.ProjectGroup.list(
{
query: {
only: ['project_group_id', 'name']
}
}
);

const projectGroupMap = {};
response.results.forEach((projectGroupInfo) => {
projectGroupMap[projectGroupInfo.project_group_id] = projectGroupInfo.name;
});

return projectGroupMap;
};

const makeResponse = async (params, resources, resourceType) => {
// let projectGroupMap = {};
// if (resourceType == 'identity.Project') {
// projectGroupMap = await getProjectGroupMap();
// }

const { key, name, data } = autoConfig.resourceTypes[params.resource_type].response as AutocompleteResource;
const results = resources.results.map((resource) => {
const result: AutocompleteResource = {
key: resource[key],
name: ejs.render(name, resource)
};

// if (resourceType == 'identity.Project') {
// const projectGroupName = projectGroupMap[resource.project_group_id];
// if (projectGroupName)
// {
// result.name = `${projectGroupName} > ${result.name}`;
// }
// }

if (data) {
const _data = {};
Object.keys(data).forEach(key => {
Expand All @@ -116,11 +149,12 @@ const getResources = async (params) => {
checkParameter(params);
const options = getOptions(params.options);
const [service, resource] = parseResourceType(params.resource_type);
const client = await getClient(service);

const client = await getClient(service);
const requestParams = makeRequest(params, options);

const response = await client[resource].list(requestParams);
return makeResponse(params, response);
return makeResponse(params, response, params.resource_type);
};

export {
Expand Down

0 comments on commit c2ea0f9

Please sign in to comment.