Skip to content

Commit

Permalink
feat(architecture-improvements): adds transform to solutions API
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiantela committed Oct 17, 2024
1 parent 3f08eef commit 0756fab
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 32 deletions.
121 changes: 92 additions & 29 deletions src/api/solutions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,61 @@
import request from './request';
import { useAuthStore } from '@/stores/Auth';

const transform = {
globals: {
from: (
globals: {
name: string;
value: string;
}[],
): Solution['globals'] => {
return (globals || []).reduce(
(previous, { name, value }) => ({ ...previous, [name]: { value } }),
{},
);
},

to: (
globals: Solution['globals'],
): {
[key: string]: string;
} => {
return Object.keys(globals)
.map((globalName) => ({ [globalName]: globals[globalName].value }))
.reduce((previous, current) => ({ ...previous, ...current }), {});
},
},

sectors: {
from: (
sectors: {
name: string;
tags: string[];
queues?: {
name: string;
}[];
}[],
): Solution['sectors'] => {
return (sectors || []).reduce(
(previous, { name, tags }) => ({
...previous,
[name]: {
value: tags.filter((tag) => tag),
},
}),
{},
);
},

to: (sectors: Solution['sectors']): { name: string; tags: string[] }[] => {
return Object.keys(sectors).map((sectorName) => ({
name: sectorName,
tags: sectors[sectorName].value,
}));
},
},
};

export default {
async listSolutions({ category }: { category: string }): Promise<Solution[]> {
const authStore = useAuthStore();
Expand Down Expand Up @@ -61,17 +116,41 @@ export default {
} & Pick<Solution, 'sectors' | 'globals'>) {
const authStore = useAuthStore();

await request.$http.post(`/v2/feature/${solutionUuid}/integrate/`, {
const { data } = await request.$http.post<{
status: number;
data: {
description: string;
disclaimer: string;
documentation_url: string;
feature_uuid: string;
feature_version: string;
globals: {
name: string;
value: string;
}[];
integrated_on: string;
name: string;
project: string;
sectors: {
name: string;
queues: {
name: string;
}[];
tags: string[];
}[];
user: string;
};
}>(`/v2/feature/${solutionUuid}/integrate/`, {
project_uuid: authStore.projectUuid,
action_base_flow: '',
sectors: Object.keys(sectors).map((sectorName) => ({
name: sectorName,
tags: sectors[sectorName].value,
})),
globals_values: Object.keys(globals)
.map((globalName) => ({ [globalName]: globals[globalName].value }))
.reduce((previous, current) => ({ ...previous, ...current }), {}),
sectors: transform.sectors.to(sectors),
globals_values: transform.globals.to(globals),
});

return {
globals: transform.globals.from(data.data.globals),
sectors: transform.sectors.from(data.data.sectors),
};
},

async disintegrateSolution({ solutionUuid }: { solutionUuid: string }) {
Expand All @@ -93,13 +172,8 @@ export default {

await request.$http.put(`/v2/feature/${solutionUuid}/integrate/`, {
project_uuid: authStore.projectUuid,
sectors: Object.keys(sectors).map((sectorName) => ({
name: sectorName,
tags: sectors[sectorName].value,
})),
globals_values: Object.keys(globals)
.map((globalName) => ({ [globalName]: globals[globalName].value }))
.reduce((previous, current) => ({ ...previous, ...current }), {}),
sectors: transform.sectors.to(sectors),
globals_values: transform.globals.to(globals),
});
},

Expand All @@ -120,7 +194,7 @@ export default {
description: string;
disclaimer: string;
documentation_url: string;
globals?: {
globals: {
name: string;
value: string;
}[];
Expand All @@ -145,19 +219,8 @@ export default {
description: solution.description,
tip: solution.disclaimer,
documentation: solution.documentation_url,
globals: (solution.globals || []).reduce(
(previous, { name, value }) => ({ ...previous, [name]: { value } }),
{},
),
sectors: solution.sectors.reduce(
(previous, { name, tags }) => ({
...previous,
[name]: {
value: tags.filter((tag) => tag),
},
}),
{},
),
globals: transform.globals.from(solution.globals),
sectors: transform.sectors.from(solution.sectors),
}));
},
};
6 changes: 3 additions & 3 deletions src/stores/Solutions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,16 @@ export const useSolutionsStore = defineStore('solutions', () => {
const search = findSolution({ uuid });

if (search?.solution) {
await APISolutions.integrateSolution({
const response = await APISolutions.integrateSolution({
solutionUuid: search?.solution.uuid,
sectors,
globals,
});

search.integrationCorrespondent.add({
...search.solution,
sectors,
globals,
sectors: response.sectors,
globals: response.globals,
});
}
}
Expand Down

0 comments on commit 0756fab

Please sign in to comment.