Skip to content

Commit

Permalink
Enhance promotion configuration flexibility
Browse files Browse the repository at this point in the history
- Replace yes/no flag with configurable sheet name
- Make promotion config inherit from main config
- Add helper function for promotion mode checks
  • Loading branch information
halelidan committed Oct 29, 2024
1 parent fef66a3 commit 0fd4bfb
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 47 deletions.
31 changes: 18 additions & 13 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ interface Config {
'VertexAI Api Domain Part'?: string;
'Gemini Model'?: string;
'Image Generation Model'?: string;
'Is Promotion Mode': string;
'Promotion Mode Config': string;
}

export const sheet =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Config')!;
export const promotionSheet =
SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Promotion_Config')!;

const DEFAULT_CONFIG: Config = {
'Ads API Key': '',
Expand Down Expand Up @@ -83,7 +81,7 @@ const DEFAULT_CONFIG: Config = {
'VertexAI Api Domain Part': undefined,
'Gemini Model': undefined,
'Image Generation Model': undefined,
'Is Promotion Mode': 'no',
'Promotion Mode Config': '',
};

export const ADIOS_MODES = {
Expand All @@ -103,12 +101,19 @@ export const CONFIG: Config =

export const PROMOTION_CONFIG: Config =
{
...promotionSheet
?.getRange('A2:B')
.getDisplayValues()
.filter(e => e[0])
.reduce((res, e) => {
return { ...res, [e[0]]: e[1] };
}, DEFAULT_CONFIG),
'Is Promotion Mode': 'yes', // Set to 'true' for promotion config
} ?? DEFAULT_CONFIG;
...CONFIG,
...(CONFIG['Promotion Mode Config']
? SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName(CONFIG['Promotion Mode Config'])
?.getRange('A2:B')
.getDisplayValues()
.filter(e => e[0])
.reduce((res, e) => {
return { ...res, [e[0]]: e[1] };
}, {})
: {}),
} ?? CONFIG;

export const inPromotionMode = (): boolean => {
return !!CONFIG['Promotion Mode Config'];
};
4 changes: 2 additions & 2 deletions src/frontend-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { CONFIG, PROMOTION_CONFIG } from './config';
import { CONFIG, PROMOTION_CONFIG, inPromotionMode } from './config';
import { GcsApi } from './gcs-api';
import {
ImagePolicyViolations,
Expand Down Expand Up @@ -51,7 +51,7 @@ export interface Image {
selected?: boolean;
issues?: ImageIssue[];
}
const isPromotionMode = CONFIG['Is Promotion Mode'] === 'yes';
const isPromotionMode = inPromotionMode();
const config = isPromotionMode ? PROMOTION_CONFIG : CONFIG;
const gcsApi = new GcsApi(config['GCS Bucket']);

Expand Down
11 changes: 3 additions & 8 deletions src/image-extension-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { CONFIG, PROMOTION_CONFIG } from './config';
import { CONFIG, PROMOTION_CONFIG, inPromotionMode } from './config';
import { GcsApi } from './gcs-api';
import { GoogleAdsApi } from './google-ads-api';
import { Triggerable } from './triggerable';
Expand All @@ -30,11 +30,6 @@ export class ImageExtensionService extends Triggerable {
super();
this._isPromotionMode = isPromotionMode;
this._config = this._isPromotionMode ? PROMOTION_CONFIG : CONFIG;
Logger.log(
`Config sheet chosen is: ${
this._isPromotionMode ? 'PROMOTION_CONFIG' : 'CONFIG'
}`
);
this._gcsApi = new GcsApi(this._config['GCS Bucket']);
this._googleAdsApi = new GoogleAdsApi(
this._config['Ads API Key'],
Expand Down Expand Up @@ -129,7 +124,7 @@ export class ImageExtensionService extends Triggerable {
}

static triggeredRun() {
const isPromotionMode = CONFIG['Is Promotion Mode'] === 'yes';
const isPromotionMode = inPromotionMode();
Logger.log(`triggeredRun method:`);
Logger.log(`Is Promotion Mode: ${isPromotionMode}`);

Expand All @@ -142,7 +137,7 @@ export class ImageExtensionService extends Triggerable {
}

static manuallyRun() {
const isPromotionMode = CONFIG['Is Promotion Mode'] === 'yes';
const isPromotionMode = inPromotionMode();
Logger.log(`manuallyRun method:`);
Logger.log(`Is Promotion Mode: ${isPromotionMode}`);

Expand Down
16 changes: 8 additions & 8 deletions src/image-generation-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { ADIOS_MODES, CONFIG, PROMOTION_CONFIG } from './config';
import {
ADIOS_MODES,
CONFIG,
PROMOTION_CONFIG,
inPromotionMode,
} from './config';
import { GcsApi } from './gcs-api';
import { GoogleAdsApiFactory } from './google-ads-api-mock';
import { Triggerable } from './triggerable';
Expand All @@ -35,11 +40,6 @@ export class ImageGenerationService extends Triggerable {
super();
this._isPromotionMode = isPromotionMode;
this._config = this._isPromotionMode ? PROMOTION_CONFIG : CONFIG;
Logger.log(
`Config sheet chosen is: ${
this._isPromotionMode ? 'PROMOTION_CONFIG' : 'CONFIG'
}`
);
this._gcsApi = new GcsApi(this._config['GCS Bucket']);
this._vertexAiApi = new VertexAiApi(
this._config['GCP Project'],
Expand Down Expand Up @@ -337,7 +337,7 @@ export class ImageGenerationService extends Triggerable {
}

static triggeredRun() {
const isPromotionMode = CONFIG['Is Promotion Mode'] === 'yes';
const isPromotionMode = inPromotionMode();
Logger.log(`triggeredRun method:`);
Logger.log(`Is Promotion Mode: ${isPromotionMode}`);
PropertiesService.getScriptProperties().setProperty(
Expand All @@ -349,7 +349,7 @@ export class ImageGenerationService extends Triggerable {
}

static manuallyRun() {
const isPromotionMode = CONFIG['Is Promotion Mode'] === 'yes';
const isPromotionMode = inPromotionMode();
Logger.log(`manuallyRun method:`);
Logger.log(`Is Promotion Mode: ${isPromotionMode}`);
PropertiesService.getScriptProperties().setProperty(
Expand Down
11 changes: 3 additions & 8 deletions src/image-pause-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { CONFIG, PROMOTION_CONFIG } from './config';
import { CONFIG, PROMOTION_CONFIG, inPromotionMode } from './config';
import { GcsApi } from './gcs-api';
import { GoogleAdsApi } from './google-ads-api';
import { Triggerable } from './triggerable';
Expand All @@ -28,11 +28,6 @@ export class ImagePauseService extends Triggerable {
super();
this._isPromotionMode = isPromotionMode;
this._config = this._isPromotionMode ? PROMOTION_CONFIG : CONFIG;
Logger.log(
`Config sheet chosen is: ${
this._isPromotionMode ? 'PROMOTION_CONFIG' : 'CONFIG'
}`
);
this._gcsApi = new GcsApi(this._config['GCS Bucket']);
this._googleAdsApi = new GoogleAdsApi(
this._config['Ads API Key'],
Expand Down Expand Up @@ -113,7 +108,7 @@ export class ImagePauseService extends Triggerable {
}

static triggeredRun() {
const isPromotionMode = CONFIG['Is Promotion Mode'] === 'yes';
const isPromotionMode = inPromotionMode();
Logger.log(`triggeredRun method:`);
Logger.log(`Is Promotion Mode: ${isPromotionMode}`);

Expand All @@ -126,7 +121,7 @@ export class ImagePauseService extends Triggerable {
}

static manuallyRun() {
const isPromotionMode = CONFIG['Is Promotion Mode'] === 'yes';
const isPromotionMode = inPromotionMode();
Logger.log(`manuallyRun method:`);
Logger.log(`Is Promotion Mode: ${isPromotionMode}`);

Expand Down
11 changes: 3 additions & 8 deletions src/image-upload-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { CONFIG, PROMOTION_CONFIG } from './config';
import { CONFIG, PROMOTION_CONFIG, inPromotionMode } from './config';
import { GcsApi } from './gcs-api';
import { GoogleAdsApi } from './google-ads-api';
import { Triggerable } from './triggerable';
Expand All @@ -28,11 +28,6 @@ export class ImageUploadService extends Triggerable {
super();
this._isPromotionMode = isPromotionMode;
this._config = this._isPromotionMode ? PROMOTION_CONFIG : CONFIG;
Logger.log(
`Config sheet chosen is: ${
this._isPromotionMode ? 'PROMOTION_CONFIG' : 'CONFIG'
}`
);
this._gcsApi = new GcsApi(this._config['GCS Bucket']);
this._googleAdsApi = new GoogleAdsApi(
this._config['Ads API Key'],
Expand Down Expand Up @@ -106,7 +101,7 @@ export class ImageUploadService extends Triggerable {
}

static triggeredRun() {
const isPromotionMode = CONFIG['Is Promotion Mode'] === 'yes';
const isPromotionMode = inPromotionMode();
PropertiesService.getScriptProperties().setProperty(
`${ImageUploadService.name}StartTime`,
new Date().getTime().toString()
Expand All @@ -116,7 +111,7 @@ export class ImageUploadService extends Triggerable {
}

static manuallyRun() {
const isPromotionMode = CONFIG['Is Promotion Mode'] === 'yes';
const isPromotionMode = inPromotionMode();
PropertiesService.getScriptProperties().setProperty(
`${ImageUploadService.name}StartTime`,
new Date().getTime().toString()
Expand Down

0 comments on commit 0fd4bfb

Please sign in to comment.