@@ -123,22 +123,57 @@ export async function importPlan(req: Request, res: Response) {
123123 // insert all the imported activities into the plan
124124 logger . info ( `POST /importPlan: Importing activities: ${ name } ` ) ;
125125
126+ const tagsResponse = await fetch ( GQL_API_URL , {
127+ body : JSON . stringify ( {
128+ query : gql . GET_TAGS ,
129+ } ) ,
130+ headers,
131+ method : 'POST' ,
132+ } ) ;
133+
134+ const tagsResponseJSON = ( await tagsResponse . json ( ) ) as {
135+ data : {
136+ tags : Tag [ ] ;
137+ } ;
138+ } ;
139+
140+ let tagsMap : Record < string , Tag > = { } ;
141+ if ( tagsResponseJSON != null && tagsResponseJSON . data != null ) {
142+ const {
143+ data : { tags } ,
144+ } = tagsResponseJSON ;
145+ tagsMap = tags . reduce ( ( prevTagsMap : Record < string , Tag > , tag ) => {
146+ return {
147+ ...prevTagsMap ,
148+ [ tag . name ] : tag ,
149+ } ;
150+ } , { } ) ;
151+ }
152+
153+ // derive a map of uniquely named tags from the list of activities that doesn't already exist in the database
126154 const activityTags = activities . reduce (
127155 ( prevActivitiesTagsMap : Record < string , Pick < Tag , 'color' | 'name' > > , { tags } ) => {
128- const tagsMap =
129- tags ?. reduce ( ( prevTagsMap : Record < string , Pick < Tag , 'color' | 'name' > > , { tag } ) => {
130- return {
131- ...prevTagsMap ,
132- [ tag . name ] : {
133- color : tag . color ,
134- name : tag . name ,
135- } ,
136- } ;
137- } , { } ) ?? { } ;
156+ const currentTagsMap =
157+ tags ?. reduce (
158+ ( prevTagsMap : Record < string , Pick < Tag , 'color' | 'name' > > , { tag : { name : tagName , color } } ) => {
159+ // If the tag doesn't exist already, add it
160+ if ( tagsMap [ tagName ] === undefined ) {
161+ return {
162+ ...prevTagsMap ,
163+ [ tagName ] : {
164+ color,
165+ name : tagName ,
166+ } ,
167+ } ;
168+ }
169+ return prevTagsMap ;
170+ } ,
171+ { } ,
172+ ) ?? { } ;
138173
139174 return {
140175 ...prevActivitiesTagsMap ,
141- ...tagsMap ,
176+ ...currentTagsMap ,
142177 } ;
143178 } ,
144179 { } ,
@@ -160,35 +195,18 @@ export async function importPlan(req: Request, res: Response) {
160195 } ;
161196
162197 if ( data && data . insert_tags && data . insert_tags . returning . length ) {
198+ // track the newly created tags for cleanup if an error occurs during plan import
163199 createdTags = data . insert_tags . returning ;
164200 }
165201
166- const tagsResponse = await fetch ( GQL_API_URL , {
167- body : JSON . stringify ( {
168- query : gql . GET_TAGS ,
202+ // add the newly created tags to the `tagsMap`
203+ tagsMap = createdTags . reduce (
204+ ( prevTagsMap : Record < string , Tag > , tag ) => ( {
205+ ...prevTagsMap ,
206+ [ tag . name ] : tag ,
169207 } ) ,
170- headers,
171- method : 'POST' ,
172- } ) ;
173-
174- const tagsResponseJSON = ( await tagsResponse . json ( ) ) as {
175- data : {
176- tags : Tag [ ] ;
177- } ;
178- } ;
179-
180- let tagsMap : Record < string , Tag > = { } ;
181- if ( tagsResponseJSON != null && tagsResponseJSON . data != null ) {
182- const {
183- data : { tags } ,
184- } = tagsResponseJSON ;
185- tagsMap = tags . reduce ( ( prevTagsMap : Record < string , Tag > , tag ) => {
186- return {
187- ...prevTagsMap ,
188- [ tag . name ] : tag ,
189- } ;
190- } , { } ) ;
191- }
208+ tagsMap ,
209+ ) ;
192210
193211 const activityRemap : Record < number , number > = { } ;
194212 const activityDirectivesInsertInput = activities . map (
0 commit comments