Skip to content

Commit

Permalink
better type validation for function return types
Browse files Browse the repository at this point in the history
  • Loading branch information
Tschonti committed Jun 15, 2024
1 parent 4a56138 commit b47b288
Show file tree
Hide file tree
Showing 13 changed files with 72 additions and 46 deletions.
5 changes: 3 additions & 2 deletions packages/functions/src/functions/alerts/getAll.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions'
import { app, HttpRequest, InvocationContext } from '@azure/functions'
import { Between } from 'typeorm'
import { getUserFromHeaderAndAssertAdmin } from '../../service/auth.service'
import Alert from '../../typeorm/entities/Alert'
import { getAppDataSource } from '../../typeorm/getConfig'
import { handleException } from '../../util/handleException'
import { PontozoResponse } from '../../util/pontozoResponse'

/**
* Called when the user visits the admin frontpage. Returns all the alerts from the past 14 days.
*/
export const getAlerts = async (req: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> => {
export const getAlerts = async (req: HttpRequest, context: InvocationContext): Promise<PontozoResponse<Alert[]>> => {
try {
await getUserFromHeaderAndAssertAdmin(req, context)
const currentDate = new Date()
Expand Down
7 changes: 4 additions & 3 deletions packages/functions/src/functions/auth/user.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions'
import { app, HttpRequest, InvocationContext } from '@azure/functions'
import { DbUser } from '@pontozo/common'
import { getUserFromHeader } from '../../service/auth.service'
import UserRoleAssignment from '../../typeorm/entities/UserRoleAssignment'
import { getAppDataSource } from '../../typeorm/getConfig'
import { handleException } from '../../util/handleException'
import { PontozoResponse } from '../../util/pontozoResponse'

export const currentUser = async (req: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> => {
export const currentUser = async (req: HttpRequest, context: InvocationContext): Promise<PontozoResponse<DbUser>> => {
try {
const user = getUserFromHeader(req)
const roles = await (await getAppDataSource(context)).getRepository(UserRoleAssignment).find({ where: { userId: user.szemely_id } })
return {
jsonBody: { ...user, roles: roles.map((r) => r.role) } as DbUser,
jsonBody: { ...user, roles: roles.map((r) => r.role) },
}
} catch (error) {
return handleException(req, context, error)
Expand Down
10 changes: 7 additions & 3 deletions packages/functions/src/functions/categories/getOne.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions'
import { app, HttpRequest, InvocationContext } from '@azure/functions'
import { CategoryWithCriteria, EntityWithEditableIndicator, PontozoException } from '@pontozo/common'
import { getUserFromHeaderAndAssertAdmin } from '../../service/auth.service'
import Category from '../../typeorm/entities/Category'
import { getAppDataSource } from '../../typeorm/getConfig'
import { handleException } from '../../util/handleException'
import { PontozoResponse } from '../../util/pontozoResponse'
import { validateId } from '../../util/validation'

export const getCategory = async (req: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> => {
export const getCategory = async (
req: HttpRequest,
context: InvocationContext
): Promise<PontozoResponse<EntityWithEditableIndicator<CategoryWithCriteria>>> => {
try {
await getUserFromHeaderAndAssertAdmin(req, context)
const id = validateId(req)
Expand All @@ -23,7 +27,7 @@ export const getCategory = async (req: HttpRequest, context: InvocationContext):
.sort((ctc1, ctc2) => ctc1.order - ctc2.order)
.map((ctc) => ({ ...ctc.criterion, roles: JSON.parse(ctc.criterion.roles) })),
editable: !seasons.some(({ season }) => season.startDate < new Date()),
} as EntityWithEditableIndicator<CategoryWithCriteria>,
},
}
} catch (error) {
return handleException(req, context, error)
Expand Down
10 changes: 7 additions & 3 deletions packages/functions/src/functions/criteria/getOne.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions'
import { app, HttpRequest, InvocationContext } from '@azure/functions'
import { CriterionWithSeason, EntityWithEditableIndicator, PontozoException } from '@pontozo/common'
import { getUserFromHeaderAndAssertAdmin } from '../../service/auth.service'
import Criterion from '../../typeorm/entities/Criterion'
import { getAppDataSource } from '../../typeorm/getConfig'
import { handleException } from '../../util/handleException'
import { PontozoResponse } from '../../util/pontozoResponse'
import { validateId } from '../../util/validation'

export const getCriterion = async (req: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> => {
export const getCriterion = async (
req: HttpRequest,
context: InvocationContext
): Promise<PontozoResponse<EntityWithEditableIndicator<CriterionWithSeason>>> => {
try {
await getUserFromHeaderAndAssertAdmin(req, context)
const id = validateId(req)
Expand All @@ -23,7 +27,7 @@ export const getCriterion = async (req: HttpRequest, context: InvocationContext)
seasons: [...new Set(seasons)],
roles: JSON.parse(criterion.roles),
editable: !categories.some(({ category }) => category.seasons.some(({ season }) => season.startDate < new Date())),
} as EntityWithEditableIndicator<CriterionWithSeason>,
},
}
} catch (error) {
return handleException(req, context, error)
Expand Down
7 changes: 4 additions & 3 deletions packages/functions/src/functions/events/getOne.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions'
import { app, HttpRequest, InvocationContext } from '@azure/functions'
import { EventWithRating, PontozoException } from '@pontozo/common'
import { getUserFromHeader } from '../../service/auth.service'
import Event from '../../typeorm/entities/Event'
import EventRating from '../../typeorm/entities/EventRating'
import { getAppDataSource } from '../../typeorm/getConfig'
import { handleException } from '../../util/handleException'
import { PontozoResponse } from '../../util/pontozoResponse'
import { validateId } from '../../util/validation'

/**
* Called when the user visits an event page to get the event data and the user's rating.
*/
export const getOneEvent = async (req: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> => {
export const getOneEvent = async (req: HttpRequest, context: InvocationContext): Promise<PontozoResponse<EventWithRating>> => {
try {
const eventId = validateId(req)

Expand All @@ -29,7 +30,7 @@ export const getOneEvent = async (req: HttpRequest, context: InvocationContext):
jsonBody: {
event,
userRating,
} as EventWithRating,
},
}
} catch (error) {
return handleException(req, context, error)
Expand Down
8 changes: 5 additions & 3 deletions packages/functions/src/functions/events/getOneFromCache.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions'
import { PontozoException } from '@pontozo/common'
import { app, HttpRequest, InvocationContext } from '@azure/functions'
import { EventWithRating, PontozoException } from '@pontozo/common'
import { getRedisClient } from '../../redis/redisClient'
import { getUserFromHeader } from '../../service/auth.service'
import { handleException } from '../../util/handleException'
import { PontozoResponse } from '../../util/pontozoResponse'

/**
* NOT USED
* Called when the user visits an event page while the DB is still starting up.
* Return the data of the event from the cache, omits the user's rating since that is not stored in the cache
*/
export const getOneEventFromCache = async (req: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> => {
export const getOneEventFromCache = async (req: HttpRequest, context: InvocationContext): Promise<PontozoResponse<EventWithRating>> => {
try {
const eventId = parseInt(req.params.eventId)
if (isNaN(eventId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Event from '../../typeorm/entities/Event'
import { handleException } from '../../util/handleException'

/**
* NOT USED
* Called when the user visits the frontpage. Returns all the events that are in the cache
*/
export const getRateableEventsFromCache = async (req: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> => {
Expand Down
7 changes: 4 additions & 3 deletions packages/functions/src/functions/ratings/getEventInfo.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions'
import { app, HttpRequest, InvocationContext } from '@azure/functions'
import { CategoryWithCriteria, EventRatingInfo, isHigherRank, PontozoException } from '@pontozo/common'
import { getUserFromHeader } from '../../service/auth.service'
import Criterion from '../../typeorm/entities/Criterion'
import EventRating from '../../typeorm/entities/EventRating'
import { getAppDataSource } from '../../typeorm/getConfig'
import { handleException } from '../../util/handleException'
import { PontozoResponse } from '../../util/pontozoResponse'

/**
* Called after the users starts the rating of an event to get all the rating categories and criteria.
*/
export const getEventInfo = async (req: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> => {
export const getEventInfo = async (req: HttpRequest, context: InvocationContext): Promise<PontozoResponse<EventRatingInfo>> => {
try {
const ratingId = parseInt(req.params.id)

Expand Down Expand Up @@ -81,7 +82,7 @@ export const getEventInfo = async (req: HttpRequest, context: InvocationContext)
endDate: event.endDate,
eventCategories,
stageCategories,
} as EventRatingInfo,
},
}
} catch (error) {
return handleException(req, context, error)
Expand Down
20 changes: 11 additions & 9 deletions packages/functions/src/functions/results/getEventResults.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions'
import { app, HttpRequest, InvocationContext } from '@azure/functions'
import { EventResult, EventResultList, EventState, PontozoException } from '@pontozo/common'
import { In, LessThan } from 'typeorm'
import Category from '../../typeorm/entities/Category'
Expand All @@ -8,8 +8,9 @@ import Season from '../../typeorm/entities/Season'
import { getAppDataSource } from '../../typeorm/getConfig'
import { currentSeasonFilter } from '../../util/currentSeasonFilter'
import { handleException } from '../../util/handleException'
import { PontozoResponse } from '../../util/pontozoResponse'

export const getEventResults = async (req: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> => {
export const getEventResults = async (req: HttpRequest, context: InvocationContext): Promise<PontozoResponse<EventResultList>> => {
try {
const seasonId = req.query.get('seasonId')
const categoryIds =
Expand Down Expand Up @@ -70,14 +71,15 @@ export const getEventResults = async (req: HttpRequest, context: InvocationConte
})),
}))
const { events, ...rawSeason } = season
const resultList: EventResultList = {
season: rawSeason,
categories,
criteria: criteria.map((c) => ({ ...c, roles: JSON.parse(c.roles) })),
eventResults,
}

return { jsonBody: resultList }
return {
jsonBody: {
season: rawSeason,
categories,
criteria: criteria.map((c) => ({ ...c, roles: JSON.parse(c.roles) })),
eventResults,
},
}
} catch (error) {
return handleException(req, context, error)
}
Expand Down
7 changes: 4 additions & 3 deletions packages/functions/src/functions/results/getOne.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions'
import { EventState, PontozoException } from '@pontozo/common'
import { app, HttpRequest, InvocationContext } from '@azure/functions'
import { EventState, EventWithResults, PontozoException } from '@pontozo/common'
import { IsNull } from 'typeorm'
import { getRedisClient } from '../../redis/redisClient'
import Event from '../../typeorm/entities/Event'
import { RatingResult } from '../../typeorm/entities/RatingResult'
import { getAppDataSource } from '../../typeorm/getConfig'
import { handleException } from '../../util/handleException'
import { parseRatingResults } from '../../util/parseRatingResults'
import { PontozoResponse } from '../../util/pontozoResponse'
import { validateId } from '../../util/validation'

export const getOneResult = async (req: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> => {
export const getOneResult = async (req: HttpRequest, context: InvocationContext): Promise<PontozoResponse<EventWithResults>> => {
try {
const eventId = validateId(req)

Expand Down
24 changes: 13 additions & 11 deletions packages/functions/src/functions/seasons/getAllAndOne.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions'
import { app, HttpRequest, InvocationContext } from '@azure/functions'
import { AllSeasonsAndOne } from '@pontozo/common'
import { LessThan } from 'typeorm'
import Season from '../../typeorm/entities/Season'
import { getAppDataSource } from '../../typeorm/getConfig'
import { handleException } from '../../util/handleException'
import { PontozoResponse } from '../../util/pontozoResponse'

export const getAllAndOneSeasons = async (req: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> => {
export const getAllAndOneSeasons = async (req: HttpRequest, context: InvocationContext): Promise<PontozoResponse<AllSeasonsAndOne>> => {
try {
const now = new Date(2026, 3, 23)
const seasonId = req.query.get('seasonId')
Expand All @@ -23,17 +24,18 @@ export const getAllAndOneSeasons = async (req: HttpRequest, context: InvocationC
where: { id: selectedSeason.id },
relations: { categories: { category: { criteria: { criterion: true } } } },
})
const res: AllSeasonsAndOne = {
selectedSeason: {
...selectedSeason,
categories: selectedSeason.categories.map((stc) => ({
...stc.category,
criteria: stc.category.criteria.map((ctc) => ({ ...ctc.criterion, roles: JSON.parse(ctc.criterion.roles) })),
})),
return {
jsonBody: {
selectedSeason: {
...selectedSeason,
categories: selectedSeason.categories.map((stc) => ({
...stc.category,
criteria: stc.category.criteria.map((ctc) => ({ ...ctc.criterion, roles: JSON.parse(ctc.criterion.roles) })),
})),
},
allSeasons: seasons,
},
allSeasons: seasons,
}
return { jsonBody: res }
} catch (error) {
return handleException(req, context, error)
}
Expand Down
7 changes: 4 additions & 3 deletions packages/functions/src/functions/seasons/getOne.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { app, HttpRequest, HttpResponseInit, InvocationContext } from '@azure/functions'
import { app, HttpRequest, InvocationContext } from '@azure/functions'
import { PontozoException, SeasonWithCategories } from '@pontozo/common'
import { getUserFromHeaderAndAssertAdmin } from '../../service/auth.service'
import Season from '../../typeorm/entities/Season'
import { getAppDataSource } from '../../typeorm/getConfig'
import { handleException } from '../../util/handleException'
import { PontozoResponse } from '../../util/pontozoResponse'
import { validateId } from '../../util/validation'

export const getSeason = async (req: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> => {
export const getSeason = async (req: HttpRequest, context: InvocationContext): Promise<PontozoResponse<SeasonWithCategories>> => {
try {
await getUserFromHeaderAndAssertAdmin(req, context)
const id = validateId(req)
Expand All @@ -19,7 +20,7 @@ export const getSeason = async (req: HttpRequest, context: InvocationContext): P
jsonBody: {
...season,
categories: season.categories.sort((stc1, stc2) => stc1.order - stc2.order).map((stc) => stc.category),
} as SeasonWithCategories,
},
}
} catch (error) {
return handleException(req, context, error)
Expand Down
5 changes: 5 additions & 0 deletions packages/functions/src/util/pontozoResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { HttpResponseInit } from '@azure/functions'

export type PontozoResponse<T> = HttpResponseInit & {
jsonBody?: T
}

0 comments on commit b47b288

Please sign in to comment.