Skip to content

Commit

Permalink
Added copyPopup() operation to IPopupService.
Browse files Browse the repository at this point in the history
  • Loading branch information
azaslonov committed Apr 6, 2024
1 parent d903526 commit f5ae583
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/popups/IPopupService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ export interface IPopupService {
*/
updatePopup(popup: PopupContract): Promise<void>;

/**
* Makes a copy of specified popup.
* @param key {string} Key of the original popup.
*/
copyPopup(key: string): Promise<PopupContract>;

/**
* Returns popup content by specified key.
* @param popupKey {string} Unique popup identifier, e.g. `popups/1bbf57f8-8954-46bb-9c33-5b54643f9376`.
Expand Down
45 changes: 44 additions & 1 deletion src/popups/popupService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as Utils from "../utils";
import * as Constants from "../constants";
import * as Objects from "../objects";
import { PopupContract } from "../popups/popupContract";
import { IPopupService } from "../popups/IPopupService";
import { IObjectStorage, Query, Page } from "../persistence";
Expand All @@ -8,6 +9,7 @@ import { ILocaleService } from "../localization";
import { PopupMetadata } from "./popupMetadata";
import { PopupLocalizedContract } from "./popupLocalizedContract";
import { IBlockService } from "../blocks";
import { Logger } from "../logging";

const popupsPath = "popups";
const documentsPath = "files";
Expand All @@ -17,7 +19,8 @@ export class PopupService implements IPopupService {
constructor(
private readonly objectStorage: IObjectStorage,
private readonly localeService: ILocaleService,
private readonly blockService: IBlockService
private readonly blockService: IBlockService,
private readonly logger: Logger
) { }

public async getPopupByKey(key: string, requestedLocale?: string): Promise<PopupContract> {
Expand Down Expand Up @@ -151,6 +154,46 @@ export class PopupService implements IPopupService {
return popupContract;
}

public async copyPopup(key: string): Promise<PopupContract> {
const originalPage = await this.objectStorage.getObject<PopupLocalizedContract>(key);
const identifier = Utils.guid();
const targetKey = `${popupsPath}/${identifier}`;

const targetPage: PopupLocalizedContract = {
key: targetKey,
locales: {}
};

const sourceLocales = Object.keys(originalPage.locales);

for (const locale of sourceLocales) {
const sourceMetadata = originalPage.locales[locale];
const sourceContentKey = sourceMetadata.contentKey;
const sourcePageContent = await this.objectStorage.getObject<Contract>(sourceContentKey);

const targetIdentifier = Utils.guid();
const targetContentKey = `${documentsPath}/${targetIdentifier}`;

const targetMetadata: PopupMetadata = {
title: sourceMetadata.title + " (copy)",
description: sourceMetadata.description,
contentKey: targetContentKey
};

targetPage.locales[locale] = targetMetadata;
const targetPageContent = Objects.clone<Contract>(sourcePageContent);
targetPageContent["key"] = targetContentKey;

await this.objectStorage.addObject<Contract>(targetContentKey, targetPageContent);

this.logger.trackEvent("PopupCopied", { key: targetKey, sourcePageKey: key });
}

await this.objectStorage.addObject<PopupLocalizedContract>(targetKey, targetPage);

return this.getPopupByKey(targetKey);
}

public async updatePopup(popup: PopupContract, requestedLocale?: string): Promise<void> {
if (!popup) {
throw new Error(`Parameter "popup" not specified.`);
Expand Down
5 changes: 5 additions & 0 deletions src/search/staticSearchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export class StaticSearchService {

for (const resultBuilder of this.searchResultBuilders) {
const result = await resultBuilder.getSearchResult(rawResult.ref, searchTerm);

if (!result) {
continue;
}

results.push(result)
}
}
Expand Down

0 comments on commit f5ae583

Please sign in to comment.