|
| 1 | +/* |
| 2 | + *This migrartion is for the having only the bubbleAppUrl or streamlined using this send the bubble data to the specified destionation, the |
| 3 | + *bubbleAppUrl contains everything the version the datatype and other things, if these fields are not there we construct using the existing data |
| 4 | + */ |
| 5 | +import '../../config'; |
| 6 | +import { AppModule } from '../../app.module'; |
| 7 | + |
| 8 | +import { NestFactory } from '@nestjs/core'; |
| 9 | +import { BubbleDestinationRepository } from '@impler/dal'; |
| 10 | + |
| 11 | +export async function run() { |
| 12 | + const bubbleDestinationRepository: BubbleDestinationRepository = new BubbleDestinationRepository(); |
| 13 | + |
| 14 | + // eslint-disable-next-line no-console |
| 15 | + console.log('start migration - Constructing, Generating and Updating the bubbleAppUrl'); |
| 16 | + |
| 17 | + // Init the mongodb connection |
| 18 | + const app = await NestFactory.create(AppModule, { |
| 19 | + logger: false, |
| 20 | + }); |
| 21 | + |
| 22 | + const constructBubbleUrl = async (bubbleDestination: any | undefined | null) => { |
| 23 | + const bubbleAppUrl = [{ _id: '', bubbleAppUrl: '' }]; |
| 24 | + bubbleDestination.forEach((destination) => { |
| 25 | + /* |
| 26 | + * If no direct URL, try to construct it from available fields |
| 27 | + * Type assertion to access potential additional properties |
| 28 | + */ |
| 29 | + const customDomainName = destination.customDomainName as string | undefined; |
| 30 | + const appName = destination.appName as string | undefined; |
| 31 | + const environment = destination.environment as string | undefined; |
| 32 | + const datatype = destination.datatype as string | undefined; |
| 33 | + |
| 34 | + if (customDomainName || appName) { |
| 35 | + // Use custom domain if available, otherwise use app name with bubbleapps.io |
| 36 | + let baseUrl = customDomainName ? `https://${customDomainName}` : `https://${appName}.bubbleapps.io`; |
| 37 | + |
| 38 | + // Add version-test for development environment if specified |
| 39 | + if (environment === 'development') { |
| 40 | + baseUrl += '/version-test'; |
| 41 | + } |
| 42 | + |
| 43 | + // Construct the full URL with the data type if available |
| 44 | + if (datatype) { |
| 45 | + bubbleAppUrl.push({ _id: destination._id, bubbleAppUrl: `${baseUrl}/api/1.1/obj/${datatype}` }); |
| 46 | + } |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + return bubbleAppUrl; |
| 51 | + }; |
| 52 | + |
| 53 | + const bubbleDestinationLink = await bubbleDestinationRepository.find({ |
| 54 | + bubbleAppUrl: { $exists: true, $ne: null }, |
| 55 | + }); |
| 56 | + |
| 57 | + const bubbleDestinations = await bubbleDestinationRepository.find({}); |
| 58 | + const bubbleAppUrls = await constructBubbleUrl(bubbleDestinations); |
| 59 | + bubbleDestinationLink.map((link) => { |
| 60 | + bubbleAppUrls.push({ _id: link._id, bubbleAppUrl: link.bubbleAppUrl }); |
| 61 | + }); |
| 62 | + |
| 63 | + bubbleAppUrls.map(async (url) => { |
| 64 | + if (!url._id || !url.bubbleAppUrl) return; |
| 65 | + |
| 66 | + try { |
| 67 | + await bubbleDestinationRepository.update({ _id: url._id }, { $set: { bubbleAppUrl: url.bubbleAppUrl } }); |
| 68 | + } catch (error) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + // eslint-disable-next-line no-console |
| 74 | + console.log('end migration - Constructed, Generated and Updated the bubbleAppUrl'); |
| 75 | + |
| 76 | + app.close(); |
| 77 | + process.exit(0); |
| 78 | +} |
| 79 | +run(); |
0 commit comments