Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 21x 7x 1x 4x 2x 1x 3x 1x 2x 1x 1x 1x 3x 1x 2x 1x 1x 2x 1x 1x 3x 1x 2x 1x 1x 1x 7x 3x 4x 3x 3x 3x 1x 2x 2x 2x 1x 1x 51x 1x 36x 6x 1x 54x 1x 4x 1x 1x | import { Promotion } from "./Promotion";
import { PromotionOrder } from "./PromotionOrder";
import {
CountryType,
ApplicationRouteType,
ApplicationStatus,
YesNo,
ReviewDetail,
EarlyEndInfo,
} from "../types";
/**
* PromotionApplication aggregate root
* Represents a complete promotion application including the promotion details,
* order information, review status, and lifecycle management
*/
export class PromotionApplication {
// Basic details
private readonly applySeq: number;
private readonly countryType: CountryType;
// Merchant details
private readonly merchantId: string;
private readonly merchantName: string;
private readonly managerEmail: string;
// Application details
private readonly applicationRouteType: ApplicationRouteType;
private readonly appliedAt: Date;
private applicationStatus: ApplicationStatus;
private cancelReason: string | null;
private readonly appliedByAdmin: YesNo;
// Aggregate components
private readonly promotion: Promotion;
private readonly promotionOrder: PromotionOrder;
// Review and early termination
private reviewDetail: ReviewDetail | null;
private earlyEndInfo: EarlyEndInfo[];
private earlyEndDate: Date | null;
constructor(params: {
applySeq: number;
countryType: CountryType;
merchantId: string;
merchantName: string;
managerEmail: string;
applicationRouteType: ApplicationRouteType;
appliedAt: Date;
applicationStatus: ApplicationStatus;
cancelReason?: string | null;
appliedByAdmin: YesNo;
promotion: Promotion;
promotionOrder: PromotionOrder;
reviewDetail?: ReviewDetail | null;
earlyEndInfo?: EarlyEndInfo[];
earlyEndDate?: Date | null;
}) {
this.applySeq = params.applySeq;
this.countryType = params.countryType;
this.merchantId = params.merchantId;
this.merchantName = params.merchantName;
this.managerEmail = params.managerEmail;
this.applicationRouteType = params.applicationRouteType;
this.appliedAt = params.appliedAt;
this.applicationStatus = params.applicationStatus;
this.cancelReason = params.cancelReason ?? null;
this.appliedByAdmin = params.appliedByAdmin;
this.promotion = params.promotion;
this.promotionOrder = params.promotionOrder;
this.reviewDetail = params.reviewDetail ?? null;
this.earlyEndInfo = params.earlyEndInfo ?? [];
this.earlyEndDate = params.earlyEndDate ?? null;
}
/**
* Checks if the promotion is currently active based on current date
*/
public isActive(currentDate: Date = new Date()): boolean {
return (
this.applicationStatus === "IN_SERVICE" &&
currentDate >= this.promotion.getStartDate() &&
currentDate <= this.promotion.getEndDate()
);
}
/**
* Checks if the application is currently in service
*/
public isInService(): boolean {
return this.applicationStatus === "IN_SERVICE";
}
/**
* Checks if the application is still being processed
*/
public isApplying(): boolean {
return this.applicationStatus === "APPLYING";
}
/**
* Checks if the application has been cancelled
*/
public isCancelled(): boolean {
return this.applicationStatus === "CANCELLED";
}
/**
* Checks if the application has been completed
*/
public isCompleted(): boolean {
return this.applicationStatus === "COMPLETED";
}
/**
* Checks if the application was applied by an admin
*/
public isAppliedByAdmin(): boolean {
return this.appliedByAdmin === "Y";
}
/**
* Checks if the application has been reviewed
*/
public hasReview(): boolean {
return this.reviewDetail !== null;
}
/**
* Checks if the promotion has an early end date
*/
public hasEarlyEndDate(): boolean {
return this.earlyEndDate !== null;
}
/**
* Checks if there are early end requests
*/
public hasEarlyEndRequests(): boolean {
return this.earlyEndInfo.length > 0;
}
/**
* Cancels the application with a reason
*/
public cancel(reason: string): void {
if (!reason || reason.trim().length === 0) {
throw new Error("Cancel reason cannot be empty");
}
if (this.isCancelled()) {
throw new Error("Application is already cancelled");
}
this.applicationStatus = "CANCELLED";
this.cancelReason = reason;
}
/**
* Marks the application as in service
*/
public approve(): void {
if (this.applicationStatus !== "APPLYING") {
throw new Error("Can only approve applications with APPLYING status");
}
if (!this.promotionOrder.isPaymentCompleted()) {
throw new Error("Cannot approve application without completed payment");
}
this.applicationStatus = "IN_SERVICE";
}
/**
* Completes the application
*/
public complete(): void {
if (!this.isInService()) {
throw new Error("Can only complete applications that are in service");
}
this.applicationStatus = "COMPLETED";
}
/**
* Adds a review to the application
*/
public addReview(review: ReviewDetail): void {
this.reviewDetail = review;
}
/**
* Requests early termination of the promotion
*/
public requestEarlyEnd(earlyEndInfo: EarlyEndInfo, earlyEndDate: Date): void {
if (!this.isInService()) {
throw new Error("Can only request early end for applications in service");
}
if (earlyEndDate <= new Date()) {
throw new Error("Early end date must be in the future");
}
this.earlyEndInfo.push(earlyEndInfo);
this.earlyEndDate = earlyEndDate;
}
/**
* Updates the promotion title, only when application status is APPLYING
*/
public updateTitle(title: string): void {
if (this.applicationStatus !== "APPLYING") {
throw new Error("Can only update title when application status is APPLYING");
}
this.promotion.updateTitle(title);
}
/**
* Gets the number of days since application
*/
public getDaysSinceApplication(currentDate: Date = new Date()): number {
const diffTime = currentDate.getTime() - this.appliedAt.getTime();
return Math.floor(diffTime / (1000 * 60 * 60 * 24));
}
/**
* Checks if the application and order information are consistent
*/
public validateConsistency(): boolean {
// Check that promotion type matches
const promotionType = this.promotion.getPromotionType();
const orderPromotionType =
this.promotionOrder.getPaymentInfo().promotionType;
if (promotionType !== orderPromotionType) {
return false;
}
// Check that distribution type matches
const distributionType = this.promotion.getDistributionType();
const orderDistributionType =
this.promotionOrder.getPaymentInfo().distributionType;
if (distributionType !== orderDistributionType) {
return false;
}
return true;
}
// Getters
public getApplySeq(): number {
return this.applySeq;
}
public getCountryType(): CountryType {
return this.countryType;
}
public getMerchantId(): string {
return this.merchantId;
}
public getMerchantName(): string {
return this.merchantName;
}
public getManagerEmail(): string {
return this.managerEmail;
}
public getApplicationRouteType(): ApplicationRouteType {
return this.applicationRouteType;
}
public getAppliedAt(): Date {
return new Date(this.appliedAt);
}
public getApplicationStatus(): ApplicationStatus {
return this.applicationStatus;
}
public getCancelReason(): string | null {
return this.cancelReason;
}
public getAppliedByAdmin(): YesNo {
return this.appliedByAdmin;
}
public getPromotion(): Promotion {
return this.promotion;
}
public getPromotionOrder(): PromotionOrder {
return this.promotionOrder;
}
public getReviewDetail(): ReviewDetail | null {
return this.reviewDetail ? { ...this.reviewDetail } : null;
}
public getEarlyEndInfo(): EarlyEndInfo[] {
return [...this.earlyEndInfo];
}
public getEarlyEndDate(): Date | null {
return this.earlyEndDate ? new Date(this.earlyEndDate) : null;
}
}
|