Skip to content

Commit

Permalink
more ts - fixes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
david-loe committed Jul 19, 2023
1 parent 0c44eb8 commit 5b5a3d9
Show file tree
Hide file tree
Showing 13 changed files with 236 additions and 172 deletions.
15 changes: 0 additions & 15 deletions backend/settings.json

This file was deleted.

4 changes: 2 additions & 2 deletions backend/tests/travel.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ async function getTestTravel() {
destinationPlace: { place: 'place', country: 'DE' },
travelInsideOfEU: false,
startDate: new Date(),
endDate: new Date(new Date().getTime() + 7 * 24 * 60 * 60 * 1000),
endDate: new Date(new Date().valueOf() + 7 * 24 * 60 * 60 * 1000),
advance: {
amount: 100,
currency: 'EUR',
},
claimOvernightLumpSum: true,
stages: [{
departure: new Date(),
arrival: new Date(new Date().getTime() + 3 * 60 * 60 * 1000),
arrival: new Date(new Date().valueOf() + 3 * 60 * 60 * 1000),
transport: 'airplane',
startLocation: {
country: 'DE',
Expand Down
132 changes: 93 additions & 39 deletions common/scriptsts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Currency, Locale, Money, Place, Travel } from './types'
import settings from './settings.json'

function getFlagEmoji(countryCode: string): string | null {
const noFlag = ['XCD', 'XOF', 'XAF', 'ANG', 'XPF']
Expand All @@ -13,22 +14,54 @@ function getFlagEmoji(countryCode: string): string | null {
return String.fromCodePoint(...codePoints)
}

function datetimeToDateString(datetime: Date): string {
return new Date(datetime).toISOString().slice(0, -14)
function isValidDate(date: Date | string | number): Date | null {
const d = new Date(date)
if (isNaN(d.valueOf())) {
return null
} else {
return d
}
}

function datetimeToDateString(datetime: Date | string | number): string {
const date = isValidDate(datetime)
if (date) {
return date.toISOString().slice(0, -14)
} else {
return ''
}
}

function datetimeToDatetimeString(datetime: Date | string | number): string {
const date = isValidDate(datetime)
if (date) {
return date.toISOString().slice(0, -8)
} else {
return ''
}
}

function htmlInputStringToDateTime(dateTimeStr: string): Date | null {
const date = isValidDate(dateTimeStr)
if (date) {
return new Date(date.valueOf() - date.getTimezoneOffset() * 60 * 1000)
} else {
return null
}
}

function datetimeToDate(datetime: Date): Date {
function datetimeToDate(datetime: Date | string | number): Date {
return new Date(datetimeToDateString(datetime))
}

function getDiffInDays(startDate: Date, endDate: Date): number {
function getDiffInDays(startDate: Date | string | number, endDate: Date | string | number): number {
const firstDay = datetimeToDate(startDate)
const lastDay = datetimeToDate(endDate)
return (lastDay.valueOf() - firstDay.valueOf()) / (1000 * 60 * 60 * 24)
}

function getDayList(startDate: Date, endDate: Date): Date[] {
const days = []
function getDayList(startDate: Date | string | number, endDate: Date | string | number): Date[] {
const days: Date[] = []
for (var i = 0; i < getDiffInDays(startDate, endDate) + 1; i++) {
days.push(new Date(datetimeToDate(startDate).valueOf() + i * 1000 * 60 * 60 * 24))
}
Expand All @@ -41,7 +74,13 @@ function getMoneyString(
func: (x: number) => number = (x: number): number => x,
locale: Locale = 'de'
): string {
return func(useExchangeRate && money.exchangeRate ? money.exchangeRate.amount : money.amount).toLocaleString(locale, {
var amount = 0
if (useExchangeRate && money.exchangeRate) {
amount = money.exchangeRate.amount
} else if (money.amount != null) {
amount = money.amount
}
return func(amount).toLocaleString(locale, {
style: 'currency',
currency:
useExchangeRate && money.exchangeRate
Expand All @@ -56,7 +95,7 @@ function getDetailedMoneyString(money: Money, locale: Locale, printZero = false)
if (!money || (money && (typeof money.amount !== 'number' || (!money.amount && !printZero)))) {
return ''
}
var str = money.amount.toLocaleString(locale, {
var str = money.amount!.toLocaleString(locale, {
style: 'currency',
currency: (money.currency as Currency) ? (money.currency as Currency)._id : (money.currency as string)
})
Expand All @@ -76,50 +115,60 @@ function placeToString(place: Place, locale: Locale = 'de'): string {
return place.place + ', ' + place.country.name[locale] + place.country.flag
}

function dateToTimeString(date: Date): string {
if (!date) return ''
const dateObject = new Date(date)
const hour = dateObject.getUTCHours().toString().padStart(2, '0')
const minute = dateObject.getUTCMinutes().toString().padStart(2, '0')
return hour + ':' + minute
function dateToTimeString(date: string | number | Date): string {
const dateObject = isValidDate(date)
if (dateObject) {
const hour = dateObject.getUTCHours().toString().padStart(2, '0')
const minute = dateObject.getUTCMinutes().toString().padStart(2, '0')
return hour + ':' + minute
} else {
return ''
}
}

function datetoDateString(date: Date): string {
if (!date) return ''
const dateObject = new Date(date)
const month = (dateObject.getUTCMonth() + 1).toString().padStart(2, '0')
const day = dateObject.getUTCDate().toString().padStart(2, '0')
return day + '.' + month
function datetoDateString(date: string | number | Date): string {
const dateObject = isValidDate(date)
if (dateObject) {
const month = (dateObject.getUTCMonth() + 1).toString().padStart(2, '0')
const day = dateObject.getUTCDate().toString().padStart(2, '0')
return day + '.' + month
} else {
return ''
}
}

function dateTimeToString(datetime: Date): string {
function dateTimeToString(datetime: string | number | Date): string {
return datetoDateString(datetime) + ' ' + dateToTimeString(datetime)
}

function datetoDateStringWithYear(date: Date): string {
if (!date) return ''
const dateObject = new Date(date)
const year = dateObject.getUTCFullYear().toString()
const month = (dateObject.getUTCMonth() + 1).toString().padStart(2, '0')
const day = dateObject.getUTCDate().toString().padStart(2, '0')
return day + '.' + month + '.' + year
function datetoDateStringWithYear(date: string | number | Date): string {
const dateObject = isValidDate(date)
if (dateObject) {
const year = dateObject.getUTCFullYear().toString()
const month = (dateObject.getUTCMonth() + 1).toString().padStart(2, '0')
const day = dateObject.getUTCDate().toString().padStart(2, '0')
return day + '.' + month + '.' + year
} else {
return ''
}
}

function getLumpSumsSum(travel: Travel): Money {
var sum = 0
for (const day of travel.days) {
for (const refund of day.refunds) {
sum += refund.refund.amount
if (refund.refund.amount != null) {
sum += refund.refund.amount
}
}
}
// baseCurrency
return { amount: sum, currency: { _id: 'EUR' } }
return { amount: sum, currency: settings.baseCurrency }
}

function getExpensesSum(travel: Travel): Money {
var sum = 0
for (const stage of travel.stages) {
if (stage.cost && stage.cost.amount > 0) {
if (stage.cost && stage.cost.amount != null) {
if (stage.cost.exchangeRate && typeof stage.cost.exchangeRate.amount == 'number') {
sum += stage.cost.exchangeRate.amount
} else {
Expand All @@ -128,32 +177,36 @@ function getExpensesSum(travel: Travel): Money {
}
}
for (const expense of travel.expenses) {
if (expense.cost && expense.cost.amount > 0) {
if (expense.cost && expense.cost.amount != null) {
if (expense.cost.exchangeRate && typeof expense.cost.exchangeRate.amount == 'number') {
sum += expense.cost.exchangeRate.amount
} else {
sum += expense.cost.amount
}
}
}
// baseCurrency
return { amount: sum, currency: { _id: 'EUR' } }
return { amount: sum, currency: settings.baseCurrency }
}

function getTravelTotal(travel: Travel): Money {
var advance = 0
if (travel.advance && travel.advance.amount) {
if (travel.advance && travel.advance.amount != null) {
advance = travel.advance.exchangeRate ? travel.advance.exchangeRate.amount : travel.advance.amount
}
// baseCurrency
return { amount: getExpensesSum(travel).amount + getLumpSumsSum(travel).amount - advance, currency: { _id: 'EUR' } }
return { amount: getExpensesSum(travel).amount! + getLumpSumsSum(travel).amount! - advance, currency: settings.baseCurrency }
}

function clone<T>(object: T): T {
return JSON.parse(JSON.stringify(object))
}

export {
getFlagEmoji,
getDiffInDays,
getDayList,
datetimeToDateString,
datetimeToDatetimeString,
htmlInputStringToDateTime,
getMoneyString,
getDetailedMoneyString,
placeToString,
Expand All @@ -163,5 +216,6 @@ export {
datetoDateStringWithYear,
getLumpSumsSum,
getExpensesSum,
getTravelTotal
getTravelTotal,
clone
}
2 changes: 1 addition & 1 deletion common/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"lunchCateringLumpSumCut": 0.4,
"dinnerCateringLumpSumCut": 0.4,
"toleranceStageDatesToApprovedTravelDates": 3,
"baseCurrency": { "_id": "EUR", "symbol": "" },
"baseCurrency": { "_id": "EUR", "symbol": "", "name": { "de": "Euro", "en": "euro" }, "subunit": "Cent" },
"allowSpouseRefund": true,
"refundPerKM": 0.3,
"version": 0,
Expand Down
27 changes: 18 additions & 9 deletions common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ export interface Place {
country: CountrySimple
}

export interface DocumentFile {
data?: Blob
type: 'image/jpeg' | 'image/png' | 'application/pdf'
name: string
_id?: string
}

export interface UserSimple {
uid: string
email: string
Expand All @@ -70,7 +77,7 @@ export interface User extends UserSimple {
}

export interface Money {
amount: number
amount: number | null
currency: Currency | string
exchangeRate?: {
date: Date
Expand All @@ -80,26 +87,28 @@ export interface Money {
}

export interface Cost extends Money {
receipts: Array<string>
date: Date
receipts: Array<DocumentFile>
date: Date | string
}

export interface Stage {
departure: Date
arrival: Date
departure: Date | string
arrival: Date | string
startLocation: Place
endLocation: Place
midnightCountries?: [{ date: Date; country: CountrySimple }]
distance?: number
midnightCountries?: { date: Date; country: CountrySimple }[]
distance?: number | null
transport: 'ownCar' | 'airplane' | 'shipOrFerry' | 'otherTransport'
cost: Cost
purpose: 'professional' | 'mixed' | 'private'
_id: string
}

export interface Expense {
description: string
cost: Cost
purpose: 'professional' | 'mixed'
_id: string
}

export interface TravelSimple {
Expand All @@ -111,8 +120,8 @@ export interface TravelSimple {
reason: string
destinationPlace: Place
travelInsideOfEU: boolean
startDate: Date
endDate: Date
startDate: Date | string
endDate: Date | string
advance: Money
progress: number
_id: string
Expand Down
22 changes: 4 additions & 18 deletions frontend/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ import axios from 'axios'
import { defineComponent } from 'vue'
import { Locale, User } from '../../common/types'
import { languages } from '../../common/settings.json'
import TwemojiCountryFlags from '../../common/fonts/TwemojiCountryFlags.woff2'
interface Alert {
type: 'danger' | 'success'
Expand All @@ -146,7 +147,8 @@ export default defineComponent({
loadState: 'UNLOADED',
loadingPromise: null as Promise<void> | null,
bp: { sm: 576, md: 768, lg: 992, xl: 1200, xxl: 1400 },
languages
languages,
TwemojiCountryFlags
}
},
methods: {
Expand Down Expand Up @@ -268,22 +270,6 @@ export default defineComponent({
}
}, 5000)
},
// Handling all dates as UTC
dateTimeToHTMLInputString(date: Date) {
if (!date) return ''
const dateObject = new Date(date)
return dateObject.toISOString().slice(0, -8)
},
htmlInputStringToDateTime(dateTimeStr: string) {
if (!dateTimeStr) return null
const dateObject = new Date(dateTimeStr)
return new Date(dateObject.valueOf() - dateObject.getTimezoneOffset() * 60 * 1000)
},
dateToHTMLInputString(date: Date) {
if (!date) return ''
const dateObject = new Date(date)
return dateObject.toISOString().slice(0, -14)
},
async pushSettings() {
this.user.settings.language = this.$i18n.locale as Locale
try {
Expand All @@ -306,7 +292,7 @@ export default defineComponent({
@font-face {
font-family: 'Twemoji Country Flags';
unicode-range: U+1F1E6-1F1FF, U+1F3F4, U+E0062-E0063, U+E0065, U+E0067, U+E006C, U+E006E, U+E0073-E0074, U+E0077, U+E007F;
src: url(./common/fonts/TwemojiCountryFlags.woff2) format('woff2');
src: v-bind('TwemojiCountryFlags') format('woff2');
}
body {
Expand Down
Loading

0 comments on commit 5b5a3d9

Please sign in to comment.