Skip to content

Commit

Permalink
sorted lump sum calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
david-loe committed Jul 22, 2024
1 parent bd05600 commit 63ad7e4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 81 deletions.
45 changes: 4 additions & 41 deletions backend/models/country.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { HydratedDocument, Model, Schema, model } from 'mongoose'
import { Country, LumpSum } from '../../common/types.js'
import { getSettings } from '../helper.js'
import { Schema, model } from 'mongoose'
import { Country } from '../../common/types.js'

interface Methods {
getLumpSum(date: Date, special?: string): Promise<LumpSum>
}

type CountryModel = Model<Country, {}, Methods>

export const countrySchema = new Schema<Country, CountryModel, Methods>({
export const countrySchema = new Schema<Country>({
_id: { type: String, required: true, trim: true, alias: 'code', label: 'labels.code' },
flag: { type: String },
name: {
Expand Down Expand Up @@ -48,34 +41,4 @@ export const countrySchema = new Schema<Country, CountryModel, Methods>({
}
})

countrySchema.methods.getLumpSum = async function (date: Date, special: string | undefined = undefined): Promise<LumpSum> {
if (this.lumpSumsFrom) {
return (await model('Country').findOne({ _id: this.lumpSumsFrom })).getLumpSum(date)
} else if (this.lumpSums.length == 0) {
const settings = await getSettings()
return (await model('Country').findOne({ _id: settings.travelSettings.fallBackLumpSumCountry })).getLumpSum(date)
} else {
var nearest = 0
for (var i = 0; i < this.lumpSums.length; i++) {
var diff = date.valueOf() - (this.lumpSums[i].validFrom as Date).valueOf()
if (diff >= 0 && diff < date.valueOf() - (this.lumpSums[nearest].validFrom as Date).valueOf()) {
nearest = i
}
}
if (date.valueOf() - (this.lumpSums[nearest].validFrom as Date).valueOf() < 0) {
throw new Error('No valid lumpSum found for Country: ' + this._id + ' for date: ' + date)
}
if (special && this.lumpSums[nearest].specials) {
for (const lumpSumSpecial of this.lumpSums[nearest].specials!) {
if (lumpSumSpecial.city === special) {
return lumpSumSpecial
}
}
}
return this.lumpSums[nearest]
}
}

export default model<Country, CountryModel>('Country', countrySchema)

export interface CountryDoc extends Methods, HydratedDocument<Country> {}
export default model('Country', countrySchema)
39 changes: 0 additions & 39 deletions common/lumpSumCalculator.ts

This file was deleted.

40 changes: 39 additions & 1 deletion common/travel.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import LumpSumCalculator from './lumpSumCalculator.js'
import { datetimeToDate, getDayList, getDiffInDays } from './scripts.js'
import {
baseCurrency,
Country,
CountryCode,
CountrySimple,
LumpSum,
Meal,
Place,
PurposeSimple,
Expand Down Expand Up @@ -344,3 +344,41 @@ export class TravelValidator {
return conflicts
}
}

export default class LumpSumCalculator {
fallBackLumpSumCountry: CountryCode
getCountryById: (id: CountryCode) => Promise<Country>

constructor(getCountryById: (id: CountryCode) => Promise<Country>, fallBackLumpSumCountry: CountryCode) {
this.getCountryById = getCountryById
this.fallBackLumpSumCountry = fallBackLumpSumCountry
}
async getLumpSum(country: Country, date: Date, special: string | undefined = undefined): Promise<LumpSum> {
if (country.lumpSumsFrom) {
const lumpSumFrom = await this.getCountryById(country.lumpSumsFrom)
return this.getLumpSum(lumpSumFrom, date)
} else if (country.lumpSums.length == 0) {
const fallBackLumpSumCountry = await this.getCountryById(this.fallBackLumpSumCountry)
return this.getLumpSum(fallBackLumpSumCountry, date)
} else {
var nearest = 0
for (var i = 0; i < country.lumpSums.length; i++) {
var diff = date.valueOf() - (country.lumpSums[i].validFrom as Date).valueOf()
if (diff >= 0 && diff < date.valueOf() - (country.lumpSums[nearest].validFrom as Date).valueOf()) {
nearest = i
}
}
if (date.valueOf() - (country.lumpSums[nearest].validFrom as Date).valueOf() < 0) {
throw new Error('No valid lumpSum found for Country: ' + country._id + ' for date: ' + date)
}
if (special && country.lumpSums[nearest].specials) {
for (const lumpSumSpecial of country.lumpSums[nearest].specials!) {
if (lumpSumSpecial.city === special) {
return lumpSumSpecial
}
}
}
return country.lumpSums[nearest]
}
}
}

0 comments on commit 63ad7e4

Please sign in to comment.