11import path from "path" ;
22import fs from 'fs' ;
33import { MIGRATION_DATA_CONFIG , LIST_EXTENSION_UID } from "../constants/index.js" ;
4+ import { contentMapperService } from "./contentMapper.service.js" ;
45
56const {
67 CUSTOM_MAPPER_FILE_NAME ,
@@ -27,6 +28,26 @@ const writeExtFile = async ({ destinationStackId, extensionData }: any) => {
2728 console . error ( "🚀 ~ fs.writeFile ~ err:" , writeErr ) ;
2829 }
2930}
31+ const formatExtensionData = ( extension : any , destinationStackId : string ) => {
32+ return {
33+ "stackHeaders" : { "api_key" : destinationStackId } ,
34+ "urlPath" : `/extensions/${ extension ?. uid } ` ,
35+ "uid" : extension ?. uid ,
36+ "created_at" : extension ?. created_at ,
37+ "updated_at" : extension ?. updated_at ,
38+ "created_by" : extension ?. created_by ,
39+ "updated_by" : extension ?. updated_by ,
40+ "tags" : extension ?. tags ,
41+ "_version" : extension ?. _version ,
42+ "title" : extension ?. title ,
43+ "config" : extension ?. config ,
44+ "type" : extension ?. type ,
45+ "data_type" : extension ?. data_type ,
46+ "multiple" : extension ?. multiple ,
47+ "srcdoc" : extension ?. srcdoc ,
48+
49+ }
50+ }
3051
3152const getExtension = ( { uid, destinationStackId } : any ) => {
3253 if ( uid === LIST_EXTENSION_UID ) {
@@ -50,8 +71,12 @@ const getExtension = ({ uid, destinationStackId }: any) => {
5071 }
5172 return null ;
5273}
74+ const getExsitingExtension = async ( { existingStackId, token_payload } : any ) => {
75+ const result = await contentMapperService . getExistingExtensions ( { existingStackId, token_payload} ) ;
76+ return result ;
77+ }
5378
54- const createExtension = async ( { destinationStackId } : any ) => {
79+ const createExtension = async ( { destinationStackId, existingStackId , token_payload } : any ) => {
5580 const extensionPath = path . join ( MIGRATION_DATA_CONFIG . DATA , destinationStackId , CUSTOM_MAPPER_FILE_NAME ) ;
5681 const extMapper : any = await fs . promises . readFile ( extensionPath , "utf-8" ) . catch ( async ( ) => { } ) ;
5782 if ( extMapper !== undefined ) {
@@ -66,6 +91,21 @@ const createExtension = async ({ destinationStackId }: any) => {
6691 }
6792 await writeExtFile ( { destinationStackId, extensionData } )
6893 }
94+ else {
95+ const existingExtension = await getExsitingExtension ( { existingStackId, token_payload } ) ;
96+ if ( existingExtension && Array ?. isArray ( existingExtension ) ) {
97+ const extensionData : any = { } ;
98+ for await ( const extension of existingExtension ) {
99+ const extData = formatExtensionData ( extension , destinationStackId ) ;
100+ if ( extData ) {
101+ extensionData [ extension ?. uid ] = extension ;
102+ }
103+ }
104+ await writeExtFile ( { destinationStackId, extensionData } )
105+
106+ }
107+
108+ }
69109}
70110
71111
0 commit comments