Skip to content

Commit

Permalink
[Backend] Type routes/feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
rikurauhala committed Jul 22, 2024
1 parent 592832e commit d8dcf16
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 26 deletions.
20 changes: 0 additions & 20 deletions services/backend/src/routes/feedback.js

This file was deleted.

34 changes: 34 additions & 0 deletions services/backend/src/routes/feedback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Router } from 'express'

import { sendFeedbackToToska } from '../services/mailService'
import { OodikoneRequest } from '../types'
import { ApplicationError } from '../util/customErrors'
import logger from '../util/logger'

const router = Router()

interface EmailRequest extends OodikoneRequest {
body: {
content: string
}
}

router.post('/email', async (req: EmailRequest) => {
const {
body: { content },
user,
} = req

if (!user) {
throw new ApplicationError('User not found', 404)
}

await sendFeedbackToToska({
feedbackContent: content,
user,
})

logger.info(`${user.userId} succesfully sent feedback to Toska`)
})

export default router
9 changes: 4 additions & 5 deletions services/backend/src/routes/login.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { Request, Response, Router } from 'express'
import { Response, Router } from 'express'
import { omit } from 'lodash'

import { FormattedUser } from '../types'
import { OodikoneRequest } from '../types'
import { ApplicationError } from '../util/customErrors'

const router = Router()

interface CustomRequest extends Request {
user?: FormattedUser
interface LoginRequest extends OodikoneRequest {
logoutUrl?: string
}

router.get('/', async (req: CustomRequest, res: Response) => {
router.get('/', async (req: LoginRequest, res: Response) => {
const { user, logoutUrl } = req

if (!user) {
Expand Down
3 changes: 2 additions & 1 deletion services/backend/src/services/mailService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from 'axios'

import { isProduction, pateToken } from '../config'
import { FormattedUser } from '../types'
import { ApplicationError } from '../util/customErrors'

const pateClient = axios.create({
Expand Down Expand Up @@ -31,7 +32,7 @@ export const sendFeedbackToToska = async ({
user,
}: {
feedbackContent: string
user: { name: string; userId: string; email: string }
user: FormattedUser
}) => {
const { name, userId, email } = user
const userDetails = `Sent by ${name}, userid: ${userId}, email: ${email}`
Expand Down
2 changes: 2 additions & 0 deletions services/backend/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ExtentCode } from './extentCode'
import { GenderCode } from './genderCode'
import { Language } from './language'
import { Name } from './name'
import { OodikoneRequest } from './oodikoneRequest'
import { Phase } from './phase'
import { PriorityCode } from './priorityCode'
import { Role } from './role'
Expand All @@ -24,6 +25,7 @@ export {
GenderCode,
Language,
Name,
OodikoneRequest,
Phase,
PriorityCode,
Role,
Expand Down
8 changes: 8 additions & 0 deletions services/backend/src/types/oodikoneRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* eslint-disable import/no-cycle */
import { Request } from 'express'

import { FormattedUser } from './user'

export interface OodikoneRequest extends Request {
user?: FormattedUser
}

0 comments on commit d8dcf16

Please sign in to comment.