Skip to content

Commit

Permalink
be more explicit as to what new tags are created
Browse files Browse the repository at this point in the history
  • Loading branch information
duranb committed Oct 2, 2024
1 parent 7f104aa commit 275fd4d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 41 deletions.
6 changes: 1 addition & 5 deletions src/packages/plan/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,7 @@ export default {
`,
CREATE_TAGS: `#graphql
mutation CreateTags($tags: [tags_insert_input!]!) {
insert_tags(objects: $tags, on_conflict: {
constraint: tags_name_key,
update_columns: []
}) {
affected_rows
insert_tags(objects: $tags) {
returning {
color
created_at
Expand Down
90 changes: 54 additions & 36 deletions src/packages/plan/plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,57 @@ export async function importPlan(req: Request, res: Response) {
// insert all the imported activities into the plan
logger.info(`POST /importPlan: Importing activities: ${name}`);

const tagsResponse = await fetch(GQL_API_URL, {
body: JSON.stringify({
query: gql.GET_TAGS,
}),
headers,
method: 'POST',
});

const tagsResponseJSON = (await tagsResponse.json()) as {
data: {
tags: Tag[];
};
};

let tagsMap: Record<string, Tag> = {};
if (tagsResponseJSON != null && tagsResponseJSON.data != null) {
const {
data: { tags },
} = tagsResponseJSON;
tagsMap = tags.reduce((prevTagsMap: Record<string, Tag>, tag) => {
return {
...prevTagsMap,
[tag.name]: tag,
};
}, {});
}

// derive a map of uniquely named tags from the list of activities that doesn't already exist in the database
const activityTags = activities.reduce(
(prevActivitiesTagsMap: Record<string, Pick<Tag, 'color' | 'name'>>, { tags }) => {
const tagsMap =
tags?.reduce((prevTagsMap: Record<string, Pick<Tag, 'color' | 'name'>>, { tag }) => {
return {
...prevTagsMap,
[tag.name]: {
color: tag.color,
name: tag.name,
},
};
}, {}) ?? {};
const currentTagsMap =
tags?.reduce(
(prevTagsMap: Record<string, Pick<Tag, 'color' | 'name'>>, { tag: { name: tagName, color } }) => {
// If the tag doesn't exist already, add it
if (tagsMap[tagName] === undefined) {
return {
...prevTagsMap,
[tagName]: {
color,
name: tagName,
},
};
}
return prevTagsMap;
},
{},
) ?? {};

return {
...prevActivitiesTagsMap,
...tagsMap,
...currentTagsMap,
};
},
{},
Expand All @@ -160,35 +195,18 @@ export async function importPlan(req: Request, res: Response) {
};

if (data && data.insert_tags && data.insert_tags.returning.length) {
// track the newly created tags for cleanup if an error occurs during plan import
createdTags = data.insert_tags.returning;
}

const tagsResponse = await fetch(GQL_API_URL, {
body: JSON.stringify({
query: gql.GET_TAGS,
// add the newly created tags to the `tagsMap`
tagsMap = createdTags.reduce(
(prevTagsMap: Record<string, Tag>, tag) => ({
...prevTagsMap,
[tag.name]: tag,
}),
headers,
method: 'POST',
});

const tagsResponseJSON = (await tagsResponse.json()) as {
data: {
tags: Tag[];
};
};

let tagsMap: Record<string, Tag> = {};
if (tagsResponseJSON != null && tagsResponseJSON.data != null) {
const {
data: { tags },
} = tagsResponseJSON;
tagsMap = tags.reduce((prevTagsMap: Record<string, Tag>, tag) => {
return {
...prevTagsMap,
[tag.name]: tag,
};
}, {});
}
tagsMap,
);

const activityRemap: Record<number, number> = {};
const activityDirectivesInsertInput = activities.map(
Expand Down

0 comments on commit 275fd4d

Please sign in to comment.