Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use of services for all block fixtures #1265

Closed
wants to merge 9 commits into from
1 change: 1 addition & 0 deletions demo/api/src/db/fixtures/assets/svgs/comet-color-claim.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions demo/api/src/db/fixtures/assets/svgs/comet-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
17 changes: 11 additions & 6 deletions demo/api/src/db/fixtures/fixtures.console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Command, Console } from "nestjs-console";
import { LinkFixtureService } from "./generators/link-fixture.service";
import { PageFixtureService } from "./generators/page-fixture.service";
import { PublicUploadsFixtureService } from "./generators/public-uploads-fixture.service";
import { VideoFixtureService } from "./generators/video-fixture.service";

@Injectable()
@Console()
Expand All @@ -21,7 +22,7 @@ export class FixturesConsole {
private readonly pageFixtureService: PageFixtureService,
private readonly linkFixtureService: LinkFixtureService,
private readonly imageFixtureService: ImageFixtureService,

private readonly videoFixtureService: VideoFixtureService,
private readonly orm: MikroORM,
private readonly publicUploadsFixtureService: PublicUploadsFixtureService,
) {}
Expand All @@ -35,6 +36,11 @@ export class FixturesConsole {
// ensure repeatable runs
faker.seed(123456);

const scope: PageTreeNodeScope = {
domain: "main",
language: "en",
};

const damFilesDirectory = `${this.config.blob.storageDirectoryPrefix}-files`;
if (await this.blobStorageBackendService.folderExists(damFilesDirectory)) {
await this.blobStorageBackendService.removeFolder(damFilesDirectory);
Expand All @@ -51,13 +57,12 @@ export class FixturesConsole {
await migrator.up();

console.log("Generate Images...");
await this.imageFixtureService.generateImages(10);
await this.imageFixtureService.generateImages(10, { domain: scope.domain });

console.log("Generate Videos...");
await this.videoFixtureService.generateVideos({ domain: scope.domain });

console.log("Generate Page Tree...");
const scope: PageTreeNodeScope = {
domain: "main",
language: "en",
};

const { node } = await this.pageFixtureService.generatePage({ name: "Home", scope });
await this.pageFixtureService.generatePage({ name: "Sub", scope, parentId: node.id });
Expand Down
54 changes: 52 additions & 2 deletions demo/api/src/db/fixtures/fixtures.module.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,67 @@
import { Module } from "@nestjs/common";
import { ConfigModule } from "@src/config/config.module";
import { FixturesConsole } from "@src/db/fixtures/fixtures.console";
import { ImageFixtureService } from "@src/db/fixtures/generators/image-fixture.service";
import { LinksModule } from "@src/links/links.module";
import { PagesModule } from "@src/pages/pages.module";
import { ConsoleModule } from "nestjs-console";

import { AnchorBlockFixtureService } from "./generators/blocks/anchor-block-fixture.service";
import { ColumnsBlockFixtureService } from "./generators/blocks/columns-block-fixture.service";
import { DamImageBlockFixtureService } from "./generators/blocks/dam-image-block-fixture.service";
import { DamVideoBlockFixtureService } from "./generators/blocks/dam-video-block-fixture.service";
import { FullWidthImageBlockFixtureService } from "./generators/blocks/full-width-image-block-fixture.service";
import { HeadlineBlockFixtureService } from "./generators/blocks/headline-block-fixture.service";
import { LinkBlockFixtureService } from "./generators/blocks/link-block-fixture.service";
import { LinkListBlockFixtureService } from "./generators/blocks/link-list-block-fixture.service";
import { MediaBlockFixtureService } from "./generators/blocks/media-block-fixture.service";
import { PageContentBlockFixtureService } from "./generators/blocks/page-content-block-fixture.service";
import { PixelImageBlockFixtureService } from "./generators/blocks/pixel-image-block-fixture.service";
import { RichTextBlockFixtureService } from "./generators/blocks/richtext-block-fixture.service";
import { SeoBlockFixtureService } from "./generators/blocks/seo-block-fixture.service";
import { SpaceBlockFixtureService } from "./generators/blocks/space-block-fixture.service";
import { SvgImageBlockFixtureService } from "./generators/blocks/svg-image-block-fixture.service";
import { TextImageBlockFixtureService } from "./generators/blocks/text-image-block-fixture.service";
import { TextLinkBlockFixtureService } from "./generators/blocks/text-link-block-fixture.service";
import { TwoListsBlockFixtureService } from "./generators/blocks/two-lists-block-fixture.service";
import { VideoBlockFixtureService } from "./generators/blocks/video-block-fixture.service";
import { YouTubeVideoBlockFixtureService } from "./generators/blocks/you-tube-video-block-fixture.service";
import { ImageFixtureService } from "./generators/image-fixture.service";
import { LinkFixtureService } from "./generators/link-fixture.service";
import { PageFixtureService } from "./generators/page-fixture.service";
import { PublicUploadsFixtureService } from "./generators/public-uploads-fixture.service";
import { VideoFixtureService } from "./generators/video-fixture.service";

@Module({
imports: [ConfigModule, ConsoleModule, PagesModule, LinksModule],
providers: [FixturesConsole, PageFixtureService, LinkFixtureService, ImageFixtureService, PublicUploadsFixtureService],
providers: [
FixturesConsole,
PageFixtureService,
LinkFixtureService,
PublicUploadsFixtureService,
VideoFixtureService,
ImageFixtureService,
DamVideoBlockFixtureService,
PageContentBlockFixtureService,
RichTextBlockFixtureService,
DamImageBlockFixtureService,
SeoBlockFixtureService,
TextImageBlockFixtureService,
SpaceBlockFixtureService,
HeadlineBlockFixtureService,
AnchorBlockFixtureService,
FullWidthImageBlockFixtureService,
LinkBlockFixtureService,
LinkListBlockFixtureService,
YouTubeVideoBlockFixtureService,
DamVideoBlockFixtureService,
ColumnsBlockFixtureService,
LinkBlockFixtureService,
TextLinkBlockFixtureService,
SvgImageBlockFixtureService,
PixelImageBlockFixtureService,
MediaBlockFixtureService,
VideoBlockFixtureService,
TwoListsBlockFixtureService,
],
})
export class FixturesModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ExtractBlockInputFactoryProps } from "@comet/blocks-api";
import { AnchorBlock } from "@comet/cms-api";
import { Injectable } from "@nestjs/common";
import { random } from "faker";

@Injectable()
export class AnchorBlockFixtureService {
async generateBlockInput(): Promise<ExtractBlockInputFactoryProps<typeof AnchorBlock>> {
return {
name: random.word(),
};
}
}
37 changes: 0 additions & 37 deletions demo/api/src/db/fixtures/generators/blocks/blocks.generator.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { ExtractBlockInputFactoryProps } from "@comet/blocks-api";
import { Injectable } from "@nestjs/common";
import { ColumnsBlock, supportedBlocks } from "@src/pages/blocks/columns.block";
import { datatype, random } from "faker";

import { ColumnsContentBlock } from "../../../../pages/blocks/columns.block";
import { DamImageBlockFixtureService } from "./dam-image-block-fixture.service";
import { HeadlineBlockFixtureService } from "./headline-block-fixture.service";
import { BlockFixture } from "./page-content-block-fixture.service";
import { RichTextBlockFixtureService } from "./richtext-block-fixture.service";
import { SpaceBlockFixtureService } from "./space-block-fixture.service";

@Injectable()
export class ColumnsBlockFixtureService {
constructor(
private readonly richtextBlockFixtureService: RichTextBlockFixtureService,
private readonly damImageBlockFixtureService: DamImageBlockFixtureService,
private readonly spaceBlockFixtureService: SpaceBlockFixtureService,
private readonly headlineBlockFixtureService: HeadlineBlockFixtureService,
) {}

async generateBlockInput(): Promise<ExtractBlockInputFactoryProps<typeof ColumnsBlock>> {
const layoutTypes = ["one-column", "two-columns"];
const columnCount = datatype.number({ min: 1, max: 2 });
const layout = layoutTypes[columnCount - 1];
const columns = [];

for (let i = 0; i < columnCount; i++) {
const blocks: ExtractBlockInputFactoryProps<typeof ColumnsContentBlock>["blocks"] = [];

const supportedBlocksFixtureGenerators: Record<keyof typeof supportedBlocks, BlockFixture> = {
space: this.spaceBlockFixtureService,
richtext: this.richtextBlockFixtureService,
headline: this.headlineBlockFixtureService,
image: this.damImageBlockFixtureService,
};

for (let i = 0; i < datatype.number(20); i++) {
const [type, generator] = random.arrayElement(Object.entries(supportedBlocksFixtureGenerators));
if (generator) {
const props = await generator.generateBlockInput();

blocks.push({
key: String(i),
visible: datatype.boolean(),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type: type as any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
props: props as any,
});
}
}
columns.push({ key: datatype.uuid(), visible: datatype.boolean(), props: { blocks } });
}

return {
columns,
layout,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { ExtractBlockInputFactoryProps } from "@comet/blocks-api";
import { DamImageBlock } from "@comet/cms-api";
import { Injectable } from "@nestjs/common";
import { random } from "faker";

import { PixelImageBlockFixtureService } from "./pixel-image-block-fixture.service";
import { SvgImageBlockFixtureService } from "./svg-image-block-fixture.service";

@Injectable()
export class DamImageBlockFixtureService {
constructor(
private readonly svgImageBlockFixtureService: SvgImageBlockFixtureService,
private readonly pixelImageBlockFixtureService: PixelImageBlockFixtureService,
) {}

async generateBlockInput(): Promise<ExtractBlockInputFactoryProps<typeof DamImageBlock>> {
const types = ["svgImage", "pixelImage"] as const;
const type = random.arrayElement(types);

switch (type) {
case "svgImage":
return {
attachedBlocks: [{ type, props: await this.svgImageBlockFixtureService.generateBlockInput() }],
activeType: type,
};
case "pixelImage":
return {
attachedBlocks: [{ type, props: await this.pixelImageBlockFixtureService.generateBlockInput() }],
activeType: type,
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ExtractBlockInputFactoryProps } from "@comet/blocks-api";
import { DamVideoBlock } from "@comet/cms-api";
import { Injectable } from "@nestjs/common";
import { datatype } from "faker";

import { VideoFixtureService } from "../video-fixture.service";

@Injectable()
export class DamVideoBlockFixtureService {
constructor(private readonly videoFixtureService: VideoFixtureService) {}

async generateBlockInput(): Promise<ExtractBlockInputFactoryProps<typeof DamVideoBlock>> {
const autoplay = datatype.boolean();
const damFileId = this.videoFixtureService.getRandomVideo().id;

return {
autoplay,
loop: datatype.boolean(),
showControls: !autoplay,
damFileId,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ExtractBlockInputFactoryProps } from "@comet/blocks-api";
import { Injectable } from "@nestjs/common";
import { FullWidthImageBlock, FullWidthImageContentBlock } from "@src/pages/blocks/full-width-image.block";
import { datatype } from "faker";

import { DamImageBlockFixtureService } from "./dam-image-block-fixture.service";
import { RichTextBlockFixtureService } from "./richtext-block-fixture.service";

@Injectable()
export class FullWidthImageBlockFixtureService {
constructor(
private readonly damImageBlockFixtureService: DamImageBlockFixtureService,
private readonly richTextBlockFixtureService: RichTextBlockFixtureService,
) {}

async generateBlockInput(): Promise<ExtractBlockInputFactoryProps<typeof FullWidthImageBlock>> {
return {
image: await this.damImageBlockFixtureService.generateBlockInput(),
content: FullWidthImageContentBlock.blockInputFactory({
visible: datatype.boolean(),
block: await this.richTextBlockFixtureService.generateBlockInput(),
}),
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { ExtractBlockInputFactoryProps } from "@comet/blocks-api";
import { Injectable } from "@nestjs/common";
import { RichTextBlock } from "@src/common/blocks/rich-text.block";
import { HeadlineBlock, HeadlineLevel } from "@src/pages/blocks/headline.block";
import { datatype, random } from "faker";

import { RichTextBlockFixtureService } from "./richtext-block-fixture.service";

@Injectable()
export class HeadlineBlockFixtureService {
constructor(private readonly richtextBlockFixtureService: RichTextBlockFixtureService) {}

async generateBlockInput(): Promise<ExtractBlockInputFactoryProps<typeof HeadlineBlock>> {
const richTextBlocks: ExtractBlockInputFactoryProps<typeof RichTextBlock>["draftContent"]["blocks"] = [];

const keys = ["5jda2", "bifh7", "er118", "37lco", "5e7g4"];

for (let i = 0; i < datatype.number({ min: 1, max: 5 }); i++) {
richTextBlocks.push({
key: keys[i],
text: random.words(datatype.number({ min: 1, max: 20 })),
type: "header-one",
depth: 0,
inlineStyleRanges: [],
entityRanges: [],
data: {},
});
}

return {
headline: await this.richtextBlockFixtureService.generateBlockInput(1, richTextBlocks),
eyebrow: random.words(datatype.number({ min: 1, max: 5 })),
level: random.arrayElement(Object.values(HeadlineLevel)),
};
}
}
Loading