Skip to content

Commit ccd8de5

Browse files
author
Mayur
committed
feat: Add migration to build bubbleAppUrl and use it to send bubble data
1 parent 9cfabc9 commit ccd8de5

File tree

3 files changed

+88
-18
lines changed

3 files changed

+88
-18
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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();

apps/queue-manager/src/consumers/send-bubble-data.consumer.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export class SendBubbleDataConsumer extends BaseConsumer {
4242
const uploadId = data.uploadId;
4343
const cachedData = data.cache || (await this.getInitialCachedData(uploadId));
4444

45-
if (cachedData && cachedData.bubbleUrl) {
45+
if (cachedData && cachedData.bubbleAppUrl) {
4646
// Get valid data information
4747
let allDataJson: null | any[] = null;
4848
if (cachedData.allDataFilePath) {
@@ -67,7 +67,7 @@ export class SendBubbleDataConsumer extends BaseConsumer {
6767
uploadId,
6868
page,
6969
method: 'POST',
70-
url: cachedData.bubbleUrl,
70+
url: cachedData.bubbleAppUrl,
7171
headers: {
7272
Authorization: `Bearer ${cachedData.apiPrivateKey}`,
7373
'Content-Type': 'text/plain',
@@ -76,7 +76,7 @@ export class SendBubbleDataConsumer extends BaseConsumer {
7676

7777
await this.makeResponseEntry(
7878
response,
79-
{ bubbleAppUrl: cachedData.bubbleUrl, datatype: cachedData.datatype },
79+
{ bubbleAppUrl: cachedData.bubbleAppUrl, datatype: cachedData.datatype },
8080
cachedData.name,
8181
cachedData.email
8282
);
@@ -142,21 +142,12 @@ export class SendBubbleDataConsumer extends BaseConsumer {
142142
const templateData = await this.templateRepository.findById(uploadata._templateId, 'name');
143143

144144
const bubbleDestination = await this.bubbleDestinationRepository.findOne({ _templateId: uploadata._templateId });
145-
let bubbleUrl = bubbleDestination?.bubbleAppUrl;
146-
147-
if (
148-
!bubbleUrl &&
149-
'baseUrl' in bubbleDestination &&
150-
'datatype' in bubbleDestination &&
151-
bubbleDestination.baseUrl &&
152-
bubbleDestination.datatype
153-
) {
154-
const baseUrl = bubbleDestination.baseUrl as string;
155-
bubbleUrl = `${baseUrl}/api/1.1/obj/${bubbleDestination.datatype}`;
156-
}
145+
if (!bubbleDestination) return null;
146+
147+
const bubbleAppUrl = bubbleDestination.bubbleAppUrl;
157148

158149
// If still no URL, return null as we can't proceed without a valid URL
159-
if (!bubbleUrl) {
150+
if (!bubbleAppUrl) {
160151
return null;
161152
}
162153

@@ -179,7 +170,7 @@ export class SendBubbleDataConsumer extends BaseConsumer {
179170

180171
return {
181172
page: 1,
182-
bubbleUrl,
173+
bubbleAppUrl,
183174
chunkSize: 500,
184175
email: userEmail,
185176
datatype: bubbleDestination.datatype,

libs/shared/src/types/upload/upload.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export type SendBubbleCachedData = {
8484
email: string;
8585
datatype: string;
8686
chunkSize: number;
87-
bubbleUrl: string;
87+
bubbleAppUrl: string;
8888
apiPrivateKey: string;
8989
_templateId: string;
9090
recordFormat: string;

0 commit comments

Comments
 (0)