Skip to content

Commit

Permalink
Merge pull request #81 from david-loe/refactor/oo
Browse files Browse the repository at this point in the history
Refactor Travel Calculation
  • Loading branch information
david-loe authored Jul 22, 2024
2 parents fe32fc5 + 63ad7e4 commit dafed39
Show file tree
Hide file tree
Showing 22 changed files with 758 additions and 663 deletions.
7 changes: 5 additions & 2 deletions backend/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import mongoose from 'mongoose'
import swaggerUi from 'swagger-ui-express'
import auth from './auth.js'
import { errorHandler } from './controller/error.js'
import './db.js'
import { connectDB } from './db.js'
import { RegisterRoutes } from './dist/routes.js'
import swaggerDocument from './dist/swagger.json' assert { type: 'json' }
import i18n from './i18n.js'
import './migrations.js'
import { checkForMigrations } from './migrations.js'

await connectDB()
await checkForMigrations()

const app = express()

Expand Down
4 changes: 2 additions & 2 deletions backend/controller/projectController.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Request as ExRequest } from 'express'
import { Body, Delete, Get, Post, Queries, Query, Request, Route, Security, Tags } from 'tsoa'
import { Project as IProject, ProjectSimple, _id } from '../../common/types.js'
import { getSettings } from '../helper.js'
import Project from '../models/project.js'
import Settings from '../models/settings.js'
import { Controller, GetterQuery, SetterBody } from './controller.js'
import { AuthorizationError } from './error.js'

Expand All @@ -12,7 +12,7 @@ import { AuthorizationError } from './error.js'
export class ProjectController extends Controller {
@Get()
public async getProject(@Queries() query: GetterQuery<ProjectSimple>, @Request() request: ExRequest) {
const settings = (await Settings.findOne().lean())!
const settings = await getSettings()
if (
!settings.userCanSeeAllProjects &&
!request.user?.access['approve/travel'] &&
Expand Down
12 changes: 8 additions & 4 deletions backend/controller/uploadController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import multer from 'multer'
import fs from 'node:fs/promises'
import { Body, Consumes, Controller, Get, Middlewares, Post, Produces, Query, Request, Route, SuccessResponse, Tags } from 'tsoa'
import { _id } from '../../common/types.js'
import { documentFileHandler } from '../helper.js'
import { documentFileHandler, getSettings } from '../helper.js'
import i18n from '../i18n.js'
import Settings from '../models/settings.js'
import Token from '../models/token.js'
import User from '../models/user.js'
import { AuthorizationError, NotFoundError } from './error.js'
Expand All @@ -28,8 +27,13 @@ export class UploadController extends Controller {
@Middlewares(validateToken)
@Produces('text/html')
@SuccessResponse(200)
public async uploadPage(@Request() req: ExRequest, @Query() userId: string, @Query() tokenId: string, @Query() ownerId?: string): Promise<void> {
const settings = (await Settings.findOne().lean())!
public async uploadPage(
@Request() req: ExRequest,
@Query() userId: string,
@Query() tokenId: string,
@Query() ownerId?: string
): Promise<void> {
const settings = await getSettings()
const user = await User.findOne({ _id: userId }).lean()
const template = await fs.readFile('./templates/upload.ejs', { encoding: 'utf-8' })
const url = new URL(process.env.VITE_BACKEND_URL + '/upload/new')
Expand Down
95 changes: 49 additions & 46 deletions backend/data/settings.json
Original file line number Diff line number Diff line change
@@ -1,48 +1,41 @@
{
"accessIcons": {
"user": ["bi-card-list"],
"inWork:expenseReport": ["bi-coin", "bi-plus"],
"inWork:healthCareCost": ["bi-hospital", "bi-plus"],
"appliedFor:travel": ["bi-airplane", "bi-plus"],
"approved:travel": ["bi-airplane", "bi-plus", "bi-calendar-check"],
"admin": ["bi-person-fill"],
"appliedFor:travel": ["bi-airplane", "bi-plus"],
"approve/travel": ["bi-calendar-check"],
"confirm/healthCareCost": ["bi-hospital", "bi-check-square"],
"approved:travel": ["bi-airplane", "bi-calendar-check", "bi-plus"],
"confirm/healthCareCost": ["bi-check-square", "bi-hospital"],
"examine/expenseReport": ["bi-coin", "bi-pencil-square"],
"examine/healthCareCost": ["bi-hospital", "bi-pencil-square"],
"examine/travel": ["bi-airplane", "bi-pencil-square"]
"examine/travel": ["bi-airplane", "bi-pencil-square"],
"inWork:expenseReport": ["bi-coin", "bi-plus"],
"inWork:healthCareCost": ["bi-hospital", "bi-plus"],
"user": ["bi-card-list"]
},
"defaultAccess": {
"user": true,
"inWork:expenseReport": true,
"inWork:healthCareCost": true,
"appliedFor:travel": true,
"approved:travel": false,
"admin": false,
"appliedFor:travel": true,
"approve/travel": false,
"approved:travel": false,
"confirm/healthCareCost": false,
"examine/expenseReport": false,
"examine/healthCareCost": false,
"examine/travel": false
"examine/travel": false,
"inWork:expenseReport": true,
"inWork:healthCareCost": true,
"user": true
},
"allowSpouseRefund": false,
"userCanSeeAllProjects": true,
"breakfastCateringLumpSumCut": 0.2,
"dinnerCateringLumpSumCut": 0.4,
"factorCateringLumpSum": 1,
"factorCateringLumpSumExceptions": [],
"factorOvernightLumpSum": 1,
"factorOvernightLumpSumExceptions": [],
"fallBackLumpSumCountry": "LU",
"lunchCateringLumpSumCut": 0.4,
"maxTravelDayCount": 92,
"distanceRefunds": {
"car": 0.3,
"motorcycle": 0.2,
"halfCar": 0.15
"disableReportType": {
"expenseReport": false,
"healthCareCost": false,
"travel": false
},
"retentionPolicy": {
"deleteApprovedTravelAfterXDaysUnused": 0,
"deleteInWorkReportsAfterXDaysUnused": 0,
"deleteRefundedAfterXDays": 0,
"mailXDaysBeforeDeletion": 7
},
"secoundNightOnAirplaneLumpSumCountry": "AT",
"secoundNightOnShipOrFerryLumpSumCountry": "LU",
"stateColors": {
"appliedFor": {
"color": "#cae5ff",
Expand Down Expand Up @@ -73,21 +66,31 @@
"text": "white"
}
},
"toleranceStageDatesToApprovedTravelDates": 3,
"allowTravelApplicationForThePast": false,
"vehicleRegistrationWhenUsingOwnCar": "optional",
"uploadTokenExpireAfterSeconds": 600,
"disableReportType": {
"travel": false,
"expenseReport": false,
"healthCareCost": false
},
"retentionPolicy": {
"deleteRefundedAfterXDays": 0,
"deleteApprovedTravelAfterXDaysUnused": 0,
"deleteInWorkReportsAfterXDaysUnused": 0,
"mailXDaysBeforeDeletion": 7
"travelSettings": {
"allowSpouseRefund": false,
"allowTravelApplicationForThePast": false,
"distanceRefunds": {
"car": 0.3,
"halfCar": 0.15,
"motorcycle": 0.2
},
"factorCateringLumpSum": 1,
"factorCateringLumpSumExceptions": [],
"factorOvernightLumpSum": 1,
"factorOvernightLumpSumExceptions": [],
"fallBackLumpSumCountry": "LU",
"lumpSumCut": {
"breakfast": 0.2,
"dinner": 0.4,
"lunch": 0.4
},
"maxTravelDayCount": 92,
"secoundNightOnAirplaneLumpSumCountry": "AT",
"secoundNightOnShipOrFerryLumpSumCountry": "LU",
"toleranceStageDatesToApprovedTravelDates": 3,
"vehicleRegistrationWhenUsingOwnCar": "optional"
},

"version": "1.2.0"
"uploadTokenExpireAfterSeconds": 600,
"userCanSeeAllProjects": true,
"version": "1.2.1"
}
2 changes: 0 additions & 2 deletions backend/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import HealthInsurance from './models/healthInsurance.js'
import Organisation from './models/organisation.js'
import Project from './models/project.js'

await connectDB()

export async function connectDB() {
const first = mongoose.connection.readyState === 0
if (first) {
Expand Down
16 changes: 14 additions & 2 deletions backend/helper.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { NextFunction, Request, Response } from 'express'
import fs from 'fs/promises'
import { Model, Types } from 'mongoose'
import mongoose, { Model, Types } from 'mongoose'
import {
AnyState,
DocumentFile as IDocumentFile,
ExpenseReport as IExpenseReport,
HealthCareCost as IHealthCareCost,
Travel as ITravel,
reportIsHealthCareCost,
reportIsTravel
reportIsTravel,
Settings
} from '../common/types.js'
import { connectDB } from './db.js'
import DocumentFile from './models/documentFile.js'
import ExpenseReport from './models/expenseReport.js'
import HealthCareCost from './models/healthCareCost.js'
Expand Down Expand Up @@ -174,3 +176,13 @@ export async function getDateOfSubmission(
}
return null
}

export async function getSettings(): Promise<Settings> {
await connectDB()
const settings = (await mongoose.connection.collection('settings').findOne()) as Settings | null
if (settings) {
return settings
} else {
throw Error('Settings not found')
}
}
Loading

0 comments on commit dafed39

Please sign in to comment.