From 8144e5b7b0ae8085a5f638d61b9c1cd42fd3c6cf Mon Sep 17 00:00:00 2001 From: Dilpreet Singh Dang Date: Thu, 5 Dec 2024 17:38:10 +0530 Subject: [PATCH] DIlpreet | Test | Custom Domain Task --- apps/web-giddh/src/app/app.module.ts | 96 ++++++++++++++++++- apps/web-giddh/src/app/home/home.component.ts | 4 +- apps/web-giddh/webpack.partial.js | 24 ++--- tools/web/postbuild.js | 46 +++++++++ 4 files changed, 155 insertions(+), 15 deletions(-) diff --git a/apps/web-giddh/src/app/app.module.ts b/apps/web-giddh/src/app/app.module.ts index 57d3b71813b..ddca80021da 100644 --- a/apps/web-giddh/src/app/app.module.ts +++ b/apps/web-giddh/src/app/app.module.ts @@ -1,6 +1,6 @@ import { APP_BASE_HREF } from '@angular/common'; import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http'; -import { ErrorHandler, NgModule } from '@angular/core'; +import { APP_INITIALIZER, ErrorHandler, NgModule } from '@angular/core'; import { FormsModule, ReactiveFormsModule } from '@angular/forms'; import { BrowserModule } from '@angular/platform-browser'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; @@ -73,6 +73,93 @@ if (giddhRegion === "UK") { localStorage.setItem("Country-Region", "GL"); } +// Temporary config object +const tempConfig = { + "status": "success", + "body": { + "googleClientId": "641015054140-uj0d996itggsesgn4okg09jtn8mp0omu.apps.googleusercontent.com", + "googleClientSecret": "8htr7iQVXfZp_n87c99-jm7a", + "otpWidgetId": "33686b716134333831313239", + "otpWidgetToken": "205968TmXguUAwoD633af103P1", + "giddhWhiteLabel": { + "companyName": "Giddh", + "domainName": "test.giddh.com", + "apiDomainName": "apitest.giddh.com", + "adminDomainName": "vtest.giddh.com", + "archiveStatus": "UNARCHIVED", + "portalDomainName": "master.d2n1i21e52r793.amplifyapp.com", + "supportedDomains": [ + "localhost", + "stage.giddh.com", + "vtest.giddh.com", + "test.giddh.com" + ] + } + } +}; + +// Set temporary cookie +function setTempCookie() { + const cookieValue = encodeURIComponent(JSON.stringify(tempConfig)); + document.cookie = `giddh_config=${cookieValue}; path=/`; +} + +// Simple function to get cookie config +function getCookieConfig() { + // Set temporary cookie if it doesn't exist + if (!document.cookie.includes('giddh_config=')) { + setTempCookie(); + } + + try { + const cookie = document.cookie + .split('; ') + .find(row => row.startsWith('giddh_config=')); + return cookie ? JSON.parse(decodeURIComponent(cookie.split('=')[1])) : null; + } catch (e) { + console.error('Error parsing cookie:', e); + return null; + } +} + +// Function to update environment variables +function initializeEnvironment(): () => void { + return () => { + const config = getCookieConfig()?.body; + console.log('config',config); + if (config) { + // Update all environment variables at once + const envUpdates = { + 'AppUrl': `https://${config.giddhWhiteLabel.domainName}/`, + 'ApiUrl': `https://${config.giddhWhiteLabel.apiDomainName}/`, + 'GOOGLE_CLIENT_ID': config.googleClientId, + 'GOOGLE_CLIENT_SECRET': config.googleClientSecret, + 'OTP_WIDGET_ID': config.otpWidgetId, + 'OTP_TOKEN_AUTH': config.otpWidgetToken, + 'PORTAL_URL': `https://${config.giddhWhiteLabel.portalDomainName}/` + }; + + // Update both window and process.env variables + Object.entries(envUpdates).forEach(([key, value]) => { + (window as any)[key] = value; + (window as any)[`process.env.${key}`] = value; + }); + } + }; +} + +// Function to create service config +function createServiceConfig() { + const config = getCookieConfig()?.body; + return { + apiUrl: config?.giddhWhiteLabel?.apiDomainName + ? `https://${config.giddhWhiteLabel.apiDomainName}/` + : Configuration.ApiUrl, + appUrl: config?.giddhWhiteLabel?.domainName + ? `https://${config.giddhWhiteLabel.domainName}/` + : Configuration.AppUrl + }; +} /** * `AppModule` is the main entry point into Angular2's bootstraping process */ @@ -124,9 +211,14 @@ if (giddhRegion === "UK") { environment.ENV_PROVIDERS, APP_PROVIDERS, WindowRef, + { + provide: APP_INITIALIZER, + useFactory: initializeEnvironment, + multi: true + }, { provide: ServiceConfig, - useValue: { apiUrl: localStorage.getItem('Country-Region') === 'GB' ? Configuration.UkApiUrl : Configuration.ApiUrl, appUrl: Configuration.AppUrl, _ } + useFactory: createServiceConfig }, { provide: HTTP_INTERCEPTORS, diff --git a/apps/web-giddh/src/app/home/home.component.ts b/apps/web-giddh/src/app/home/home.component.ts index b31482edccc..5ca0148d49d 100644 --- a/apps/web-giddh/src/app/home/home.component.ts +++ b/apps/web-giddh/src/app/home/home.component.ts @@ -41,8 +41,10 @@ export class HomeComponent implements OnInit, OnDestroy { } public ngOnInit() { + console.log(ApiUrl, GOOGLE_CLIENT_ID); + this.companyUniqueName = this.generalService.companyUniqueName; - + this.needsToRedirectToLedger$.pipe(take(1)).subscribe(result => { if (result) { this.accountService.getFlattenAccounts('', '').pipe(takeUntil(this.destroyed$)).subscribe(data => { diff --git a/apps/web-giddh/webpack.partial.js b/apps/web-giddh/webpack.partial.js index 1228da6f961..30fa3680926 100644 --- a/apps/web-giddh/webpack.partial.js +++ b/apps/web-giddh/webpack.partial.js @@ -11,8 +11,8 @@ module.exports = { 'isElectron': JSON.stringify(false), 'errlyticsNeeded': JSON.stringify(false), 'errlyticsKey': JSON.stringify(''), - 'AppUrl': JSON.stringify('http://localhost:3000/'), - 'ApiUrl': JSON.stringify('https://apitest.giddh.com/'), + 'AppUrl': 'window.APP_URL || ' + JSON.stringify('http://localhost:3000/'), + 'ApiUrl': 'window.API_URL || ' + JSON.stringify('https://apitest.giddh.com/'), 'UkApiUrl': JSON.stringify('https://gbapi.giddh.com/'), 'PORTAL_URL': JSON.stringify('https://master.d2n1i21e52r793.amplifyapp.com/'), 'APP_FOLDER': JSON.stringify(''), @@ -21,26 +21,26 @@ module.exports = { 'TEST_ENV': JSON.stringify(false), 'LOCAL_ENV': JSON.stringify(true), 'enableVoucherAdjustmentMultiCurrency': JSON.stringify(true), - 'GOOGLE_CLIENT_ID': JSON.stringify(process.env.GOOGLE_CLIENT_ID_TEST), - 'GOOGLE_CLIENT_SECRET': JSON.stringify(process.env.GOOGLE_CLIENT_SECRET_TEST), + 'GOOGLE_CLIENT_ID': 'window.GOOGLE_CLIENT_ID || ' + JSON.stringify(process.env.GOOGLE_CLIENT_ID_TEST), + 'GOOGLE_CLIENT_SECRET': 'window.GOOGLE_CLIENT_SECRET || ' + JSON.stringify(process.env.GOOGLE_CLIENT_SECRET_TEST), + 'OTP_WIDGET_ID': 'window.OTP_WIDGET_ID || ' + JSON.stringify(process.env.OTP_WIDGET_ID), + 'OTP_TOKEN_AUTH': 'window.OTP_TOKEN_AUTH || ' + JSON.stringify(process.env.OTP_TOKEN_AUTH), 'RAZORPAY_KEY': JSON.stringify(process.env.RAZORPAY_KEY_TEST), 'FROALA_EDITOR_KEY': JSON.stringify(process.env.FROALA_EDITOR_KEY), - 'OTP_WIDGET_ID': JSON.stringify(process.env.OTP_WIDGET_ID), - 'OTP_TOKEN_AUTH': JSON.stringify(process.env.OTP_TOKEN_AUTH), 'process.env.enableVoucherAdjustmentMultiCurrency': JSON.stringify(true), - 'process.env.GOOGLE_CLIENT_ID': JSON.stringify(process.env.GOOGLE_CLIENT_ID_TEST), - 'process.env.GOOGLE_CLIENT_SECRET': JSON.stringify(process.env.GOOGLE_CLIENT_SECRET_TEST), + 'process.env.GOOGLE_CLIENT_ID': 'window["process.env.GOOGLE_CLIENT_ID"] || ' + JSON.stringify(process.env.GOOGLE_CLIENT_ID_TEST), + 'process.env.GOOGLE_CLIENT_SECRET': 'window["process.env.GOOGLE_CLIENT_SECRET"] || ' + JSON.stringify(process.env.GOOGLE_CLIENT_SECRET_TEST), + 'process.env.OTP_WIDGET_ID': 'window["process.env.OTP_WIDGET_ID"] || ' + JSON.stringify(process.env.OTP_WIDGET_ID), + 'process.env.OTP_TOKEN_AUTH': 'window["process.env.OTP_TOKEN_AUTH"] || ' + JSON.stringify(process.env.OTP_TOKEN_AUTH), 'process.env.RAZORPAY_KEY': JSON.stringify(process.env.RAZORPAY_KEY_TEST), 'process.env.FROALA_EDITOR_KEY': JSON.stringify(process.env.FROALA_EDITOR_KEY), - 'process.env.OTP_WIDGET_ID': JSON.stringify(process.env.OTP_WIDGET_ID), - 'process.env.OTP_TOKEN_AUTH': JSON.stringify(process.env.OTP_TOKEN_AUTH), 'process.env.ENV': JSON.stringify('development'), 'process.env.NODE_ENV': JSON.stringify('development'), 'process.env.isElectron': JSON.stringify(false), 'process.env.errlyticsNeeded': JSON.stringify(false), 'process.env.errlyticsKey': JSON.stringify(''), - 'process.env.AppUrl': JSON.stringify('http://localhost:3000/'), - 'process.env.ApiUrl': JSON.stringify('https://apitest.giddh.com/'), + 'process.env.AppUrl': 'window["process.env.AppUrl"] || ' + JSON.stringify('http://localhost:3000/'), + 'process.env.ApiUrl': 'window["process.env.ApiUrl"] || ' + JSON.stringify('https://apitest.giddh.com/'), 'process.env.UkApiUrl': JSON.stringify('https://gbapi.giddh.com/'), 'process.PORTAL_URL': JSON.stringify('https://master.d2n1i21e52r793.amplifyapp.com/'), 'process.env.APP_FOLDER': JSON.stringify('') diff --git a/tools/web/postbuild.js b/tools/web/postbuild.js index bac111bfb2f..563f436d847 100644 --- a/tools/web/postbuild.js +++ b/tools/web/postbuild.js @@ -24,6 +24,25 @@ for (var i = 0; i < process.argv.length; i++) { } const versionFilePath = path.join(__dirname, rootDirectiory, 'version.json'); +const indexFilePath = path.join(__dirname, rootDirectiory, 'index.html'); +const newIndexFilePath = path.join(__dirname, rootDirectiory, 'index.php'); + +// Add PHP script to be inserted at the beginning +const phpScript = ` +`; let mainHash = ''; let mainBundleFile = ''; @@ -45,6 +64,7 @@ readDir(path.join(__dirname, rootDirectiory)) } console.log(`Writing version and hash to ${versionFilePath}`); + console.log(`Index ${indexFilePath}`); // write current version and hash into the version.json file const src = `{"version": "${appVersion}", "hash": "${mainHash}"}`; @@ -64,6 +84,32 @@ readDir(path.join(__dirname, rootDirectiory)) const replacedFile = mainFileData.replace('{{POST_BUILD_ENTERS_HASH_HERE}}', mainHash); return writeFile(mainFilepath, replacedFile); }); + }).then(() => { + // Read index.html and convert to PHP + console.log('Converting index.html to index.php...'); + return readFile(indexFilePath, 'utf8') + .then(indexContent => { + // Combine PHP script with existing HTML content + const newContent = phpScript + indexContent; + + // Write the new index.php file + return writeFile(newIndexFilePath, newContent) + .then(() => { + console.log('Successfully created index.php'); + // Delete the original index.html + return new Promise((resolve, reject) => { + fs.unlink(indexFilePath, (err) => { + if (err) { + console.log('Error deleting index.html:', err); + reject(err); + } else { + console.log('Successfully deleted index.html'); + resolve(); + } + }); + }); + }); + }); }).catch(err => { console.log('Error with post build:', err); });