|
| 1 | +import { getLogMessage, safePromise } from "../utils/index.js"; |
| 2 | +import getAuthtoken from "../utils/auth.utils.js"; |
| 3 | +import { config } from "../config/index.js"; |
| 4 | +import https from "../utils/https.utils.js"; |
| 5 | +import fs from 'fs'; |
| 6 | +import { HTTP_TEXTS, MIGRATION_DATA_CONFIG } from "../constants/index.js"; |
| 7 | +import path from "path"; |
| 8 | +import logger from "../utils/logger.js"; |
| 9 | + |
| 10 | +const { |
| 11 | + TAXONOMIES_DIR_NAME, |
| 12 | + TAXONOMIES_FILE_NAME |
| 13 | +} = MIGRATION_DATA_CONFIG; |
| 14 | + |
| 15 | +const getDescendantsTerm = async ( {authtoken,taxonomyUid, termUid, region, stackId}: |
| 16 | + {authtoken: string,taxonomyUid : string, termUid: string, region : string, stackId : string}) => { |
| 17 | + const srcFun = "getDescendantsTerm"; |
| 18 | + |
| 19 | + try { |
| 20 | + const [err, res] = await safePromise( |
| 21 | + https({ |
| 22 | + method: "GET", |
| 23 | + url: `${config.CS_API[ |
| 24 | + region as keyof typeof config.CS_API |
| 25 | + ]!}/taxonomies/${taxonomyUid}/terms/${termUid}/descendants?include_children_count=true&include_count=true&include_order=true`, |
| 26 | + headers: { |
| 27 | + api_key : stackId, |
| 28 | + authtoken, |
| 29 | + }, |
| 30 | + })); |
| 31 | + if (err) { |
| 32 | + logger.error( |
| 33 | + getLogMessage(srcFun, HTTP_TEXTS.CS_ERROR, {}, err?.response?.data) |
| 34 | + ); |
| 35 | + |
| 36 | + return { |
| 37 | + data: err?.response?.data, |
| 38 | + status: err?.response?.status, |
| 39 | + }; |
| 40 | + } |
| 41 | + const terms = res?.data?.terms || []; |
| 42 | + const allTerms: { uid: string; name: string; parent_uid: string }[] = []; |
| 43 | + for (const term of terms) { |
| 44 | + // Push current term |
| 45 | + allTerms.push({ |
| 46 | + uid: term?.uid, |
| 47 | + name: term?.name, |
| 48 | + parent_uid: term?.parent_uid, |
| 49 | + }); |
| 50 | + |
| 51 | + // Recursively fetch deeper descendants |
| 52 | + if (term?.children_count > 0) { |
| 53 | + const nestedTerms = await getDescendantsTerm({ |
| 54 | + authtoken, |
| 55 | + taxonomyUid, |
| 56 | + termUid: term?.uid, |
| 57 | + region, |
| 58 | + stackId, |
| 59 | + }); |
| 60 | + |
| 61 | + if (Array.isArray(nestedTerms)) { |
| 62 | + allTerms.push(...nestedTerms); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + return allTerms; |
| 67 | + } catch (error) { |
| 68 | + logger.error("🚀 ~ getDescendantsTerm ~ error:", error); |
| 69 | + throw error; |
| 70 | + |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +const createTerms = async( |
| 75 | + {authtoken,taxonomyUid, region, stackId}: |
| 76 | + {authtoken: string,taxonomyUid : string, region : string, stackId : string}) => { |
| 77 | + const srcFun = "createTerms"; |
| 78 | + try { |
| 79 | + const [err, res] = await safePromise( |
| 80 | + https({ |
| 81 | + method: "GET", |
| 82 | + url: `${config.CS_API[ |
| 83 | + region as keyof typeof config.CS_API |
| 84 | + ]!}/taxonomies/${taxonomyUid}/terms?include_terms_count=true&include_count=true&include_children_count=true&include_referenced_entries_count=true`, |
| 85 | + headers: { |
| 86 | + api_key : stackId, |
| 87 | + authtoken, |
| 88 | + }, |
| 89 | + })); |
| 90 | + const termsData = res?.data?.terms; |
| 91 | + const allTerms: any[] = []; |
| 92 | + for (const term of termsData || []) { |
| 93 | + if (term?.uid) { |
| 94 | + allTerms.push({ |
| 95 | + uid: term?.uid, |
| 96 | + name: term?.name, |
| 97 | + parent_uid: term?.parent_uid, |
| 98 | + }); |
| 99 | + |
| 100 | + if (term?.children_count > 0) { |
| 101 | + const nestedTerms = await getDescendantsTerm({ |
| 102 | + authtoken, |
| 103 | + taxonomyUid, |
| 104 | + termUid: term?.uid, |
| 105 | + region, |
| 106 | + stackId, |
| 107 | + }); |
| 108 | + |
| 109 | + if (Array.isArray(nestedTerms)) { |
| 110 | + allTerms.push(...nestedTerms); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | + if (err) { |
| 120 | + logger.error( |
| 121 | + getLogMessage(srcFun, HTTP_TEXTS.CS_ERROR, {}, err?.response?.data) |
| 122 | + ); |
| 123 | + |
| 124 | + return { |
| 125 | + data: err?.response?.data, |
| 126 | + status: err?.response?.status, |
| 127 | + }; |
| 128 | + } |
| 129 | + return allTerms; |
| 130 | + |
| 131 | + } catch (error) { |
| 132 | + logger.error("🚀 ~ createTaxonomy ~ error:", error); |
| 133 | + throw error; |
| 134 | + |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | +} |
| 140 | +const createTaxonomy = async ({stackId,region,userId,current_test_stack_id} : |
| 141 | + {orgId: string, stackId: string, projectId:string,region: string,userId: string,current_test_stack_id:string}) => { |
| 142 | + const srcFun = "createTaxonomy"; |
| 143 | + const taxonomiesPath = path.join(MIGRATION_DATA_CONFIG.DATA, current_test_stack_id, TAXONOMIES_DIR_NAME); |
| 144 | + await fs.promises.mkdir(taxonomiesPath, { recursive: true }); |
| 145 | + try { |
| 146 | + const authtoken = await getAuthtoken( |
| 147 | + region, |
| 148 | + userId |
| 149 | + ); |
| 150 | + const [err, res] = await safePromise( |
| 151 | + https({ |
| 152 | + method: "GET", |
| 153 | + url: `${config.CS_API[ |
| 154 | + region as keyof typeof config.CS_API |
| 155 | + ]!}/taxonomies?include_terms_count=true&include_count=true`, |
| 156 | + headers: { |
| 157 | + api_key : stackId, |
| 158 | + authtoken, |
| 159 | + }, |
| 160 | + }) |
| 161 | + ); |
| 162 | + if (err) { |
| 163 | + logger.error( |
| 164 | + getLogMessage(srcFun, HTTP_TEXTS.CS_ERROR, {}, err?.response?.data) |
| 165 | + ); |
| 166 | + |
| 167 | + return { |
| 168 | + data: err?.response?.data, |
| 169 | + status: err?.response?.status, |
| 170 | + }; |
| 171 | + } |
| 172 | + |
| 173 | + const taxonomiesDataObject: Record<string, any> = {}; |
| 174 | + if (res?.data?.taxonomies) { |
| 175 | + for (const taxonomy of res.data.taxonomies) { |
| 176 | + if (taxonomy?.uid) { |
| 177 | + taxonomiesDataObject[taxonomy.uid] = { |
| 178 | + uid: taxonomy?.uid, |
| 179 | + name: taxonomy?.name, |
| 180 | + description: taxonomy?.description, |
| 181 | + }; |
| 182 | + const singleTaxonomy: any = {}; |
| 183 | + singleTaxonomy['taxonomy'] = { |
| 184 | + uid: taxonomy?.uid, |
| 185 | + name: taxonomy?.name, |
| 186 | + description: taxonomy?.description, |
| 187 | + }; |
| 188 | + singleTaxonomy['terms'] = await createTerms({ authtoken, taxonomyUid: taxonomy?.uid, region, stackId }); |
| 189 | + await fs.promises.writeFile(path.join(taxonomiesPath, `${taxonomy?.uid}.json`), JSON.stringify(singleTaxonomy, null, 2)); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + const filePath = path.join(taxonomiesPath, TAXONOMIES_FILE_NAME); |
| 195 | + await fs.promises.writeFile(filePath, JSON.stringify(taxonomiesDataObject, null, 2)); |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + } catch (error) { |
| 200 | + logger.error("🚀 ~ createTaxonomy ~ error:", error); |
| 201 | + throw error; |
| 202 | + } |
| 203 | + |
| 204 | +} |
| 205 | + |
| 206 | + |
| 207 | +export const taxonomyService = { |
| 208 | + createTaxonomy |
| 209 | +} |
| 210 | + |
0 commit comments