Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Billing/Transaction/Domain/Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export interface ITransactionReadDTO {

export default interface IRepository {
get(id: TransactionId): Promise<ITransactionReadDTO | null>;
save(dto: ITransactionReadDTO): Promise<void>;
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,8 @@
import type TransactionWasCreated from "@Transaction/Domain/Events/TransactionWasCreated";
import { Column, Entity, PrimaryColumn } from "typeorm";

@Entity()
export class Transactions {

public static fromCreated(event: TransactionWasCreated): Transactions {

const instance = new Transactions();

instance.uuid = event.aggregateId;
instance.product = event.product;
instance.priceAmount = Number(event.amount);
instance.priceCurrency = event.currency;
instance.createdAt = new Date();

return instance;
}

@PrimaryColumn("uuid")
public uuid: string;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import TransactionWasCreated from "@Transaction/Domain/Events/TransactionWasCreated";
import IRepository from "@Transaction/Domain/Repository";
import { EventSourcing } from "hollywood-js";
import { inject, injectable } from "inversify";
import { Transactions } from "../Mapping/Transactions";
import PostgresRepository from "../Repository/PostgresRepository";

@injectable()
export default class PostgresProjector extends EventSourcing.EventSubscriber {
constructor(
@inject("infrastructure.transaction.readModel.repository") private readonly readModel: PostgresRepository,
@inject("infrastructure.transaction.readModel.repository") private readonly readModel: IRepository,
) {
super();
}

protected async onTransactionWasCreated(event: TransactionWasCreated): Promise<void> {
try {
await this.readModel.save(Transactions.fromCreated(event));
await this.readModel.save({
createdAt: new Date(),
priceAmount: Number(event.amount),
priceCurrency: event.currency,
product: event.product,
uuid: event.aggregateId,
});
} catch (error) {
throw new Error("Error in Transaction Projector save: " + error.message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ export default class PostgresRepository implements IRepository {
@inject("infrastructure.transaction.readModel.dbal") private readonly connection: Repository<Transactions>,
) {}

public async save(transaction: Transactions): Promise<void> {
await this.connection.save(transaction);
public async save(dto: ITransactionReadDTO): Promise<void> {
const entity = new Transactions();
entity.uuid = dto.uuid;
entity.product = dto.product;
entity.priceAmount = dto.priceAmount;
entity.priceCurrency = dto.priceCurrency;
entity.createdAt = dto.createdAt;
await this.connection.save(entity);
}

public async get(id: TransactionId): Promise<ITransactionReadDTO | null> {
Expand Down
11 changes: 7 additions & 4 deletions tests/Billing/Transaction/Infrastructure/InMemoryProjector.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { EventSourcing, ReadModel } from "hollywood-js";
import { inject, injectable } from "inversify";
import TransactionWasCreated from "@Transaction/Domain/Events/TransactionWasCreated";
import {Transactions} from "@Transaction/Infrastructure/ReadModel/Mapping/Transactions";

@injectable()
export class TransactionInMemoryProjector extends EventSourcing.EventSubscriber {
Expand All @@ -13,8 +12,12 @@ export class TransactionInMemoryProjector extends EventSourcing.EventSubscriber
}

protected onTransactionWasCreated(event: TransactionWasCreated): void {
this.readModel
.save(event.aggregateId, Transactions.fromCreated(event))
;
this.readModel.save(event.aggregateId, {
createdAt: new Date(),
priceAmount: Number(event.amount),
priceCurrency: event.currency,
product: event.product,
uuid: event.aggregateId,
});
}
}