Skip to content

Commit

Permalink
Refactor term
Browse files Browse the repository at this point in the history
  • Loading branch information
mapeveri committed Jan 28, 2024
1 parent 2db0bca commit bc44c1c
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 74 deletions.
22 changes: 22 additions & 0 deletions 1706470114726-initialMigration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class InitialMigration1706470114726 implements MigrationInterface {
name = 'InitialMigration1706470114726'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE "auth_sessions" ("id" character varying NOT NULL, "session" json NOT NULL, CONSTRAINT "PK_641507381f32580e8479efc36cd" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "countries" ("id" character varying NOT NULL, "name" character varying NOT NULL, "iso" character varying NOT NULL, "languages" json NOT NULL DEFAULT '{"languages":[]}', CONSTRAINT "PK_b2d7006793e8697ab3ae2deff18" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE TABLE "term" ("id" character varying, "type" character varying, "languageId" character varying, "countryId" character varying, "userId" character varying, "created_at" TIMESTAMP DEFAULT now(), "updated_at" TIMESTAMP DEFAULT now(), "terms" json DEFAULT '{"terms":[]}', CONSTRAINT "PK_55b0479f0743f2e5d5ec414821e" PRIMARY KEY ("id"))`);
await queryRunner.query(`CREATE INDEX "IDX_19a144ebe2c3c451ea16ff1685" ON "term" ("type") `);
await queryRunner.query(`CREATE TABLE "users" ("id" character varying NOT NULL, "name" character varying NOT NULL, "provider" character varying NOT NULL, "email" character varying NOT NULL, "photo" character varying NOT NULL, "interests" text NOT NULL, CONSTRAINT "PK_a3ffb1c0c8416b9fc6f907b7433" PRIMARY KEY ("id"))`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP TABLE "users"`);
await queryRunner.query(`DROP INDEX "public"."IDX_19a144ebe2c3c451ea16ff1685"`);
await queryRunner.query(`DROP TABLE "term"`);
await queryRunner.query(`DROP TABLE "countries"`);
await queryRunner.query(`DROP TABLE "auth_sessions"`);
}

}
27 changes: 0 additions & 27 deletions migrations/1685896868634-createInitialTables.ts
Original file line number Diff line number Diff line change
@@ -1,27 +0,0 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class CreateInitialTables1685896868634 implements MigrationInterface {
name = 'CreateInitialTables1685896868634';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "countries" ("id" character varying NOT NULL, "name" character varying NOT NULL, "iso" character varying NOT NULL, "languages" json NOT NULL DEFAULT '{"languages":[]}', CONSTRAINT "PK_b2d7006793e8697ab3ae2deff18" PRIMARY KEY ("id"))`
);
await queryRunner.query(
`CREATE TABLE "expressions" ("id" character varying NOT NULL, "languageId" character varying NOT NULL, "countryId" character varying NOT NULL, "terms" json NOT NULL DEFAULT '{"terms":[]}', "userId" character varying NOT NULL, CONSTRAINT "PK_e75aa87c24b37cd86f6c8a668c9" PRIMARY KEY ("id"))`
);
await queryRunner.query(
`CREATE TABLE "users" ("id" character varying NOT NULL, "name" character varying NOT NULL, "provider" character varying NOT NULL, "email" character varying NOT NULL, "photo" character varying NOT NULL, CONSTRAINT "PK_a3ffb1c0c8416b9fc6f907b7433" PRIMARY KEY ("id"))`
);
await queryRunner.query(
`CREATE TABLE "words" ("id" character varying NOT NULL, "languageId" character varying NOT NULL, "countryId" character varying NOT NULL, "terms" json NOT NULL DEFAULT '{"terms":[]}', "userId" character varying NOT NULL, CONSTRAINT "PK_feaf97accb69a7f355fa6f58a3d" PRIMARY KEY ("id"))`
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP TABLE "words"`);
await queryRunner.query(`DROP TABLE "users"`);
await queryRunner.query(`DROP TABLE "expressions"`);
await queryRunner.query(`DROP TABLE "countries"`);
}
}
15 changes: 0 additions & 15 deletions migrations/1689433983736-addAuthSessionTable.ts
Original file line number Diff line number Diff line change
@@ -1,15 +0,0 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddAuthSessionTable1689433983736 implements MigrationInterface {
name = 'AddAuthSessionTable1689433983736';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "auth_sessions" ("id" character varying NOT NULL, "session" json NOT NULL, CONSTRAINT "PK_641507381f32580e8479efc36cd" PRIMARY KEY ("id"))`
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP TABLE "auth_sessions"`);
}
}
15 changes: 0 additions & 15 deletions migrations/1703275398290-addInterestsFieldToUser.ts
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,15 +0,0 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddInterestsFieldToUser1703275398290 implements MigrationInterface {
name = 'AddInterestsFieldToUser1703275398290';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "users" ADD "interests" text NULL`);
await queryRunner.query(`UPDATE "users" SET "interests" = ARRAY[]::varchar[]`);
await queryRunner.query(`ALTER TABLE "users" ALTER COLUMN "interests" SET NOT NULL`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "users" DROP COLUMN "interests"`);
}
}
Empty file modified migrations/1705170635561-addDatesColumnsToExpressionAndWord.ts
100755 → 100644
Empty file.
13 changes: 10 additions & 3 deletions src/languages/domain/term/expression/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ import TermId from '@src/languages/domain/term/termId';
export default class Expression extends Term {
terms: ExpressionTermCollection;

constructor(id: TermId, languageId: string, countryId: CountryId, terms: ExpressionTermCollection, userId: UserId) {
super(id, languageId, TermType.of(TermTypeEnum.EXPRESSION), countryId, userId);
constructor(
id: TermId,
languageId: string,
type: TermType,
countryId: CountryId,
terms: ExpressionTermCollection,
userId: UserId,
) {
super(id, languageId, type, countryId, userId);

this.terms = terms;
}
Expand All @@ -22,7 +29,7 @@ export default class Expression extends Term {
terms: ExpressionTermCollection,
userId: UserId,
): Expression {
const expression = new this(id, languageId, countryId, terms, userId);
const expression = new this(id, languageId, TermType.of(TermTypeEnum.EXPRESSION), countryId, terms, userId);
expression.record(
new ExpressionCreatedEvent(id.toString(), languageId, countryId.toString(), userId.toString(), terms.toArray()),
);
Expand Down
20 changes: 10 additions & 10 deletions src/languages/domain/term/termType.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { ValueObject } from '@src/shared/domain/valueObjects/valueObject';

export enum TermTypeEnum {
WORD = 'word',
EXPRESSION = 'expression',
}

export default class TermType {
readonly value: string;

constructor(type: string) {
const entry = Object.entries(TermTypeEnum).find(([, item]) => item === type);
export default class TermType extends ValueObject<string> {
constructor(value: string) {
const entry = Object.entries(TermTypeEnum).find(([, item]) => item === value);
if (!entry) {
throw new Error('Invalid type');
}

this.value = entry[1];
super(entry[1]);
}

static of(type: string): TermType {
return new this(type);
static of(value: string): TermType {
return new this(value);
}

static fromPrimitives(type: string): TermType {
return new this(type);
static fromPrimitives(value: string): TermType {
return new this(value);
}

isExpression(): boolean {
Expand Down
13 changes: 10 additions & 3 deletions src/languages/domain/term/word/word.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@ import TermId from '@src/languages/domain/term/termId';
export default class Word extends Term {
terms: WordTermCollection;

constructor(id: TermId, languageId: string, countryId: CountryId, terms: WordTermCollection, userId: UserId) {
super(id, languageId, TermType.of(TermTypeEnum.WORD), countryId, userId);
constructor(
id: TermId,
languageId: string,
type: TermType,
countryId: CountryId,
terms: WordTermCollection,
userId: UserId,
) {
super(id, languageId, type, countryId, userId);

this.terms = terms;
}

static create(id: TermId, languageId: string, countryId: CountryId, terms: WordTermCollection, userId: UserId): Word {
const word = new this(id, languageId, countryId, terms, userId);
const word = new this(id, languageId, TermType.of(TermTypeEnum.WORD), countryId, terms, userId);
word.record(
new WordCreatedEvent(id.toString(), languageId, countryId.toString(), userId.toString(), terms.toArray()),
);
Expand Down
2 changes: 1 addition & 1 deletion src/shared/domain/valueObjects/valueObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type Primitives = String | string | number | Boolean | boolean | Date;
export abstract class ValueObject<T extends Primitives> {
readonly value: T;

constructor(value: T) {
protected constructor(value: T) {
this.value = value;
}

Expand Down
3 changes: 3 additions & 0 deletions test/languages/domain/term/expression/expressionMother.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import ExpressionTermCollection from '@src/languages/domain/term/expression/expr
import { UserIdMother } from '../../user/valueObjects/userIdMother';
import TermId from '@src/languages/domain/term/termId';
import { TermIdMother } from '@test/languages/domain/term/termIdMother';
import TermType, { TermTypeEnum } from '@src/languages/domain/term/termType';

interface ExpressionMotherProps {
id?: TermId;
Expand All @@ -27,6 +28,7 @@ export default class ExpressionMother {
return new Expression(
id ?? TermIdMother.random(),
languageId ?? faker.datatype.uuid(),
TermType.of(TermTypeEnum.EXPRESSION),
countryId ?? CountryIdMother.random(),
terms ?? ExpressionTermCollectionMother.random([]),
userId ?? UserIdMother.random(),
Expand All @@ -46,6 +48,7 @@ export default class ExpressionMother {
return new Expression(
TermIdMother.random(command.id),
command.languageId,
TermType.of(TermTypeEnum.EXPRESSION),
CountryIdMother.random(command.countryId),
ExpressionTermCollectionMother.random(
terms?.map((term) => term.toPrimitives()) ?? [ExpressionTermMother.random().toPrimitives()],
Expand Down
3 changes: 3 additions & 0 deletions test/languages/domain/term/word/wordMother.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { UserIdMother } from '../../user/valueObjects/userIdMother';
import faker from 'faker';
import TermId from '@src/languages/domain/term/termId';
import { TermIdMother } from '@test/languages/domain/term/termIdMother';
import TermType, { TermTypeEnum } from '@src/languages/domain/term/termType';

interface WordMotherProps {
id?: TermId;
Expand All @@ -27,6 +28,7 @@ export default class WordMother {
return new Word(
id ?? TermIdMother.random(),
languageId ?? faker.datatype.uuid(),
TermType.of(TermTypeEnum.WORD),
countryId ?? CountryIdMother.random(),
terms ?? WordTermCollectionMother.random([]),
userId ?? UserIdMother.random(),
Expand All @@ -46,6 +48,7 @@ export default class WordMother {
return new Word(
TermIdMother.random(command.id),
command.languageId,
TermType.of(TermTypeEnum.WORD),
CountryIdMother.random(command.countryId),
WordTermCollectionMother.random(
terms?.map((term) => term.toPrimitives()) ?? [WordTermMother.random().toPrimitives()],
Expand Down

0 comments on commit bc44c1c

Please sign in to comment.