Add update method to Ad entity
This commit is contained in:
parent
62e5fd56d9
commit
3ff5277d5f
|
@ -6,6 +6,7 @@ import { AdDeletedDomainEvent } from './events/ad-delete.domain-event';
|
||||||
import { AdInvalidatedDomainEvent } from './events/ad-invalidated.domain-event';
|
import { AdInvalidatedDomainEvent } from './events/ad-invalidated.domain-event';
|
||||||
import { AdSuspendedDomainEvent } from './events/ad-suspended.domain-event';
|
import { AdSuspendedDomainEvent } from './events/ad-suspended.domain-event';
|
||||||
import { AdValidatedDomainEvent } from './events/ad-validated.domain-event';
|
import { AdValidatedDomainEvent } from './events/ad-validated.domain-event';
|
||||||
|
import { AdUpdatedDomainEvent } from './events/ad.domain-event';
|
||||||
import { ScheduleItemProps } from './value-objects/schedule-item.value-object';
|
import { ScheduleItemProps } from './value-objects/schedule-item.value-object';
|
||||||
import { WaypointProps } from './value-objects/waypoint.value-object';
|
import { WaypointProps } from './value-objects/waypoint.value-object';
|
||||||
|
|
||||||
|
@ -96,6 +97,25 @@ export class AdEntity extends AggregateRoot<AdProps> {
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
update = (newProps: CreateAdProps): AdEntity => {
|
||||||
|
this.props.driver = newProps.driver;
|
||||||
|
this.props.passenger = newProps.passenger;
|
||||||
|
this.props.frequency = newProps.frequency;
|
||||||
|
this.props.fromDate = newProps.fromDate;
|
||||||
|
this.props.toDate = newProps.toDate;
|
||||||
|
this.props.seatsProposed = newProps.seatsProposed;
|
||||||
|
this.props.seatsRequested = newProps.seatsRequested;
|
||||||
|
this.props.strict = newProps.strict;
|
||||||
|
this.props.comment = newProps.comment;
|
||||||
|
this.props.schedule = newProps.schedule.map((item) => ({ ...item }));
|
||||||
|
this.props.waypoints = newProps.waypoints.map((wp) => ({ ...wp }));
|
||||||
|
//The ad goes back to pending status until it is validated again
|
||||||
|
this.props.status = Status.PENDING;
|
||||||
|
this.addEvent(new AdUpdatedDomainEvent(this));
|
||||||
|
this.validate();
|
||||||
|
return this;
|
||||||
|
};
|
||||||
|
|
||||||
delete(): void {
|
delete(): void {
|
||||||
this.addEvent(
|
this.addEvent(
|
||||||
new AdDeletedDomainEvent({
|
new AdDeletedDomainEvent({
|
||||||
|
|
|
@ -1,23 +1,6 @@
|
||||||
import { ScheduleItemProps } from './value-objects/schedule-item.value-object';
|
import { ScheduleItemProps } from './value-objects/schedule-item.value-object';
|
||||||
import { WaypointProps } from './value-objects/waypoint.value-object';
|
import { WaypointProps } from './value-objects/waypoint.value-object';
|
||||||
|
|
||||||
// All properties that an Ad has
|
|
||||||
export interface AdProps {
|
|
||||||
userId: string;
|
|
||||||
driver: boolean;
|
|
||||||
status: Status;
|
|
||||||
passenger: boolean;
|
|
||||||
frequency: Frequency;
|
|
||||||
fromDate: string;
|
|
||||||
toDate: string;
|
|
||||||
schedule: ScheduleItemProps[];
|
|
||||||
seatsProposed: number;
|
|
||||||
seatsRequested: number;
|
|
||||||
strict: boolean;
|
|
||||||
waypoints: WaypointProps[];
|
|
||||||
comment?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Properties that are needed for an Ad creation
|
// Properties that are needed for an Ad creation
|
||||||
export interface CreateAdProps {
|
export interface CreateAdProps {
|
||||||
userId: string;
|
userId: string;
|
||||||
|
@ -34,6 +17,11 @@ export interface CreateAdProps {
|
||||||
comment?: string;
|
comment?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// All properties that an Ad has
|
||||||
|
export interface AdProps extends CreateAdProps {
|
||||||
|
status: Status;
|
||||||
|
}
|
||||||
|
|
||||||
export enum Frequency {
|
export enum Frequency {
|
||||||
PUNCTUAL = 'PUNCTUAL',
|
PUNCTUAL = 'PUNCTUAL',
|
||||||
RECURRENT = 'RECURRENT',
|
RECURRENT = 'RECURRENT',
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
import { DomainEvent } from '@mobicoop/ddd-library';
|
||||||
|
import { AdEntity } from '../ad.entity';
|
||||||
|
|
||||||
|
export abstract class AdDomainEvent extends DomainEvent {
|
||||||
|
readonly ad: AdEntity;
|
||||||
|
|
||||||
|
constructor(ad: AdEntity) {
|
||||||
|
super({
|
||||||
|
metadata: {
|
||||||
|
correlationId: ad.id,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
},
|
||||||
|
aggregateId: ad.id,
|
||||||
|
});
|
||||||
|
this.ad = ad;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class AdUpdatedDomainEvent extends AdDomainEvent {
|
||||||
|
constructor(ad: AdEntity) {
|
||||||
|
super(ad);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue