Skip to content

Commit

Permalink
create fixtures for blocks as services
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelblum committed Sep 15, 2023
1 parent 9d6a8ec commit 2665d45
Show file tree
Hide file tree
Showing 32 changed files with 779 additions and 313 deletions.
26 changes: 13 additions & 13 deletions demo/api/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -161,35 +161,35 @@ type PaginatedDamFolders {
totalCount: Int!
}

type Link implements DocumentInterface {
type Page implements DocumentInterface {
id: ID!
updatedAt: DateTime!
content: LinkBlockData!
content: PageContentBlockData!
seo: SeoBlockData!
createdAt: DateTime!
pageTreeNode: PageTreeNode
}

interface DocumentInterface {
id: ID!
updatedAt: DateTime!
}

"""Link root block data"""
scalar LinkBlockData @specifiedBy(url: "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf")
"""PageContent root block data"""
scalar PageContentBlockData @specifiedBy(url: "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf")

type Page implements DocumentInterface {
"""Seo root block data"""
scalar SeoBlockData @specifiedBy(url: "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf")

type Link implements DocumentInterface {
id: ID!
updatedAt: DateTime!
content: PageContentBlockData!
seo: SeoBlockData!
content: LinkBlockData!
createdAt: DateTime!
pageTreeNode: PageTreeNode
}

"""PageContent root block data"""
scalar PageContentBlockData @specifiedBy(url: "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf")

"""Seo root block data"""
scalar SeoBlockData @specifiedBy(url: "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf")
"""Link root block data"""
scalar LinkBlockData @specifiedBy(url: "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf")

type PageTreeNodeScope {
domain: String!
Expand Down
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.
34 changes: 13 additions & 21 deletions demo/api/src/db/fixtures/fixtures.console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,19 @@ import { EntityRepository } from "@mikro-orm/postgresql";
import { Inject, Injectable } from "@nestjs/common";
import { Config } from "@src/config/config";
import { CONFIG } from "@src/config/config.module";
import { generateSeoBlock } from "@src/db/fixtures/generators/blocks/seo.generator";
import { ImageGeneratorService } from "@src/db/fixtures/generators/image-generator.service";
import { Link } from "@src/links/entities/link.entity";
import { FileGeneratorService } from "@src/db/fixtures/generators/file-generator.service";
import { PageTreeNodeScope } from "@src/page-tree/dto/page-tree-node-scope";
import { PageTreeNodeCategory } from "@src/page-tree/page-tree-node-category";
import { PageContentBlock } from "@src/pages/blocks/page-content.block";
import { PageInput } from "@src/pages/dto/page.input";
import { Page } from "@src/pages/entities/page.entity";
import faker from "faker";
import { Command, Console } from "nestjs-console";
import slugify from "slugify";

import { PageContentBlockGeneratorService } from "./generators/blocks/page-content-block.generator";
import { SeoBlockGeneratorService } from "./generators/blocks/seo.generator";
import { LinkGeneratorService } from "./generators/link-generator.service";
import { PageGeneratorService } from "./generators/page-generator.service";

const getDefaultPageInput = (): PageInput => {
const pageInput = new PageInput();
pageInput.seo = generateSeoBlock();
pageInput.content = PageContentBlock.blockInputFactory({ blocks: [] });
return pageInput;
};

@Injectable()
@Console()
export class FixturesConsole {
Expand All @@ -35,11 +26,12 @@ export class FixturesConsole {
private readonly blobStorageBackendService: BlobStorageBackendService,
private readonly pageTreeService: PageTreeService,
private readonly pageGeneratorService: PageGeneratorService,
private readonly pageContentBlockGeneratorService: PageContentBlockGeneratorService,
private readonly seoBlockGeneratorService: SeoBlockGeneratorService,
private readonly linkGeneratorService: LinkGeneratorService,
private readonly imageGeneratorService: ImageGeneratorService,
private readonly fileGeneratorService: FileGeneratorService,
private readonly orm: MikroORM,
@InjectRepository(Page) private readonly pagesRepository: EntityRepository<Page>,
@InjectRepository(Link) private readonly linksRepository: EntityRepository<Link>,
) {}

@Command({
Expand All @@ -66,8 +58,8 @@ export class FixturesConsole {
const migrator = this.orm.getMigrator();
await migrator.up();

console.log("Generate Images...");
await this.imageGeneratorService.generateImages(10);
console.log("Generate Files...");
await this.fileGeneratorService.generateFiles(5);

console.log("Generate Page Tree...");
const scope: PageTreeNodeScope = {
Expand All @@ -90,7 +82,7 @@ export class FixturesConsole {
}
}

console.log("generate lorem ispum fixtures");
// console.log("generate lorem ispum fixtures");

const NUMBER_OF_DOMAINS_WITH_LORUM_IPSUM_CONTENT = 0; // Increase number to generate lorum ipsum fixtures

Expand Down Expand Up @@ -119,15 +111,15 @@ export class FixturesConsole {
pagesForLevel.push(page);
pagesCount++;

const pageInput = getDefaultPageInput();

const pageId = faker.datatype.uuid();

const content = await this.pageContentBlockGeneratorService.generateBlock();
const seo = await this.seoBlockGeneratorService.generateBlock();
await this.pagesRepository.persistAndFlush(
this.pagesRepository.create({
id: pageId,
content: pageInput.content.transformToBlockData(),
seo: pageInput.seo.transformToBlockData(),
content: content.transformToBlockData(),
seo: seo.transformToBlockData(),
}),
);
await this.pageTreeService.attachDocument({ id: pageId, type: "Page" }, page.id);
Expand Down
44 changes: 42 additions & 2 deletions demo/api/src/db/fixtures/fixtures.module.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,56 @@
import { Module } from "@nestjs/common";
import { ConfigModule } from "@src/config/config.module";
import { FixturesConsole } from "@src/db/fixtures/fixtures.console";
import { ImageGeneratorService } from "@src/db/fixtures/generators/image-generator.service";
import { FileGeneratorService } from "@src/db/fixtures/generators/file-generator.service";
import { LinksModule } from "@src/links/links.module";
import { PagesModule } from "@src/pages/pages.module";
import { ConsoleModule } from "nestjs-console";

import { AnchorBlockGeneratorService } from "./generators/blocks/anchor.generator";
import { ColumnsBlockGeneratorService } from "./generators/blocks/columns.generator";
import { DamImageBlockGeneratorService } from "./generators/blocks/dam-image.generator";
import { DamVideoBlockGeneratorService } from "./generators/blocks/dam-video.generator";
import { FullWidthImageBlockGeneratorService } from "./generators/blocks/full-width-image.generator";
import { HeadlineBlockGeneratorService } from "./generators/blocks/headline.generator";
import { LinkBlockGeneratorService } from "./generators/blocks/link.generator";
import { LinkListBlockGeneratorService } from "./generators/blocks/link-list.generator";
import { PageContentBlockGeneratorService } from "./generators/blocks/page-content-block.generator";
import { PixelImageBlockGeneratorService } from "./generators/blocks/pixel-image.generator";
import { RichTextBlockGeneratorService } from "./generators/blocks/richtext.generator";
import { SeoBlockGeneratorService } from "./generators/blocks/seo.generator";
import { SpaceBlockGeneratorService } from "./generators/blocks/space.generator";
import { SvgImageBlockGeneratorService } from "./generators/blocks/svg-image.generator";
import { TextImageBlockGeneratorService } from "./generators/blocks/text-image.generator";
import { TextLinkBlockGeneratorService } from "./generators/blocks/text-link.generator";
import { YouTubeVideoBlockGeneratorService } from "./generators/blocks/you-tube-video.generator";
import { LinkGeneratorService } from "./generators/link-generator.service";
import { PageGeneratorService } from "./generators/page-generator.service";

@Module({
imports: [ConfigModule, ConsoleModule, PagesModule, LinksModule],
providers: [FixturesConsole, PageGeneratorService, LinkGeneratorService, ImageGeneratorService],
providers: [
FixturesConsole,
PageGeneratorService,
LinkGeneratorService,
FileGeneratorService,
PageContentBlockGeneratorService,
RichTextBlockGeneratorService,
DamImageBlockGeneratorService,
SeoBlockGeneratorService,
TextImageBlockGeneratorService,
SpaceBlockGeneratorService,
HeadlineBlockGeneratorService,
AnchorBlockGeneratorService,
FullWidthImageBlockGeneratorService,
LinkBlockGeneratorService,
LinkListBlockGeneratorService,
YouTubeVideoBlockGeneratorService,
DamVideoBlockGeneratorService,
ColumnsBlockGeneratorService,
LinkBlockGeneratorService,
TextLinkBlockGeneratorService,
SvgImageBlockGeneratorService,
PixelImageBlockGeneratorService,
],
})
export class FixturesModule {}
13 changes: 13 additions & 0 deletions demo/api/src/db/fixtures/generators/blocks/anchor.generator.ts
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 AnchorBlockGeneratorService {
async generateBlock(): 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.

61 changes: 61 additions & 0 deletions demo/api/src/db/fixtures/generators/blocks/columns.generator.ts
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 { DamImageBlockGeneratorService } from "./dam-image.generator";
import { HeadlineBlockGeneratorService } from "./headline.generator";
import { BlockGenerator } from "./page-content-block.generator";
import { RichTextBlockGeneratorService } from "./richtext.generator";
import { SpaceBlockGeneratorService } from "./space.generator";

@Injectable()
export class ColumnsBlockGeneratorService {
constructor(
private readonly richtextGeneratorService: RichTextBlockGeneratorService,
private readonly damImageBlockGenerator: DamImageBlockGeneratorService,
private readonly spaceBlockGenerator: SpaceBlockGeneratorService,
private readonly headlineBlockGenerator: HeadlineBlockGeneratorService,
) {}

async generateBlock(): 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 blockCfg: Record<keyof typeof supportedBlocks, BlockGenerator> = {
space: this.spaceBlockGenerator,
richtext: this.richtextGeneratorService,
headline: this.headlineBlockGenerator,
image: this.damImageBlockGenerator,
};

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

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,
};
}
}
54 changes: 54 additions & 0 deletions demo/api/src/db/fixtures/generators/blocks/dam-image.generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { ExtractBlockInputFactoryProps } from "@comet/blocks-api";
import { DamImageBlock, FocalPoint, ImageCropAreaInput } from "@comet/cms-api";
import { Injectable } from "@nestjs/common";
import { datatype, random } from "faker";

import { PixelImageBlockGeneratorService } from "./pixel-image.generator";
import { SvgImageBlockGeneratorService } from "./svg-image.generator";

@Injectable()
export class DamImageBlockGeneratorService {
constructor(
private readonly svgImageBlockGenerator: SvgImageBlockGeneratorService,
private readonly pixelImageBlockGenerator: PixelImageBlockGeneratorService,
) {}

async generateBlock(): Promise<ExtractBlockInputFactoryProps<typeof DamImageBlock>> {
let type: "svgImage" | "pixelImage" = "pixelImage";
let props = await this.pixelImageBlockGenerator.generateBlock();

if (datatype.boolean()) {
type = "svgImage";
props = await this.svgImageBlockGenerator.generateBlock();
}

return {
attachedBlocks: [
{
type,
props,
},
],
activeType: type,
};
}
}

export const calculateDefaultCropInput = (): ImageCropAreaInput => {
const focalPoint = random.arrayElement([
FocalPoint.SMART,
FocalPoint.CENTER,
FocalPoint.NORTHEAST,
FocalPoint.NORTHWEST,
FocalPoint.SOUTHEAST,
FocalPoint.SOUTHWEST,
]);

return {
focalPoint,
x: focalPoint !== FocalPoint.SMART ? 0 : undefined,
y: focalPoint !== FocalPoint.SMART ? 0 : undefined,
height: focalPoint !== FocalPoint.SMART ? datatype.number({ min: 20, max: 100 }) : undefined,
width: focalPoint !== FocalPoint.SMART ? datatype.number({ min: 20, max: 100 }) : undefined,
};
};
Loading

0 comments on commit 2665d45

Please sign in to comment.