40
									
								
								src/models/DistanceDonation.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								src/models/DistanceDonation.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,40 @@
 | 
			
		||||
import { IsInt, IsNotEmpty, IsObject, IsPositive } from "class-validator";
 | 
			
		||||
import { Donation } from "./Donation";
 | 
			
		||||
import { Runner } from "./Runner";
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Defines the DistanceDonation class.
 | 
			
		||||
 * For distanceDonations a donor pledges to donate a certain amount for each kilometer ran by a runner.
 | 
			
		||||
*/
 | 
			
		||||
export class DistanceDonation extends Donation {
 | 
			
		||||
  /**
 | 
			
		||||
   * The donation's associated runner.
 | 
			
		||||
   * Used as the source of the donation's distance.
 | 
			
		||||
   */
 | 
			
		||||
  @IsObject()
 | 
			
		||||
  @IsNotEmpty()
 | 
			
		||||
  runner: Runner;
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * The donation's amount donated per distance.
 | 
			
		||||
   * The amount the donor set to be donated per kilometer that the runner ran.
 | 
			
		||||
   */
 | 
			
		||||
  @IsInt()
 | 
			
		||||
  @IsPositive()
 | 
			
		||||
  amountPerDistance: number;
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * The donation's amount in cents (or whatever your currency's smallest unit is.).
 | 
			
		||||
   * Get's calculated from the runner's distance ran and the amount donated per kilometer.
 | 
			
		||||
   */
 | 
			
		||||
  public get amount(): number {
 | 
			
		||||
    let calculatedAmount = 0;
 | 
			
		||||
    try {
 | 
			
		||||
      calculatedAmount = this.amountPerDistance * (this.runner.distance / 1000);
 | 
			
		||||
    } catch (error) {
 | 
			
		||||
      throw error;
 | 
			
		||||
    }
 | 
			
		||||
    return calculatedAmount;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										32
									
								
								src/models/Donation.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								src/models/Donation.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,32 @@
 | 
			
		||||
import {
 | 
			
		||||
  IsInt,
 | 
			
		||||
  IsNotEmpty,
 | 
			
		||||
  IsObject
 | 
			
		||||
} from "class-validator";
 | 
			
		||||
import { Donor } from './Donor';
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Defines the Donation base calss.
 | 
			
		||||
 * A donation just associates a donor with a donation amount.
 | 
			
		||||
 * The specifics of the amoun's determination has to be implemented in child classes.
 | 
			
		||||
*/
 | 
			
		||||
export abstract class Donation {
 | 
			
		||||
  /**
 | 
			
		||||
   * Autogenerated unique id (primary key).
 | 
			
		||||
   */
 | 
			
		||||
  @IsInt()
 | 
			
		||||
  id: number;
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * The donations's donor.
 | 
			
		||||
   */
 | 
			
		||||
  @IsNotEmpty()
 | 
			
		||||
  @IsObject()
 | 
			
		||||
  donor: Donor;
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * The donation's amount in cents (or whatever your currency's smallest unit is.).
 | 
			
		||||
   * The exact implementation may differ for each type of donation.
 | 
			
		||||
   */
 | 
			
		||||
  public abstract get amount(): number;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										16
									
								
								src/models/FixedDonation.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								src/models/FixedDonation.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
			
		||||
import { IsInt, IsPositive } from "class-validator";
 | 
			
		||||
import { Donation } from "./Donation";
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Defines the FixedDonation entity.
 | 
			
		||||
 * In the past there was no easy way to track fixed donations (eg. for creating donation receipts).
 | 
			
		||||
*/
 | 
			
		||||
export class FixedDonation extends Donation {
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * The donation's amount in cents (or whatever your currency's smallest unit is.).
 | 
			
		||||
   */
 | 
			
		||||
  @IsInt()
 | 
			
		||||
  @IsPositive()
 | 
			
		||||
  amount: number;
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user