1
0
Fork 0
mirror of https://github.com/betagouv/mon-entreprise synced 2025-02-09 04:05:01 +00:00
mon-entreprise/server/source/server.ts
Benjamin Arias a51920b44c
Support Usager : Implémentation de la messagerie Crisp (#2329)
* feat: Ajoute l'iframe crisp ainsi que le bloc custom html

* wip

* feat: Renomme le dossier standup.. + ajoute la fonction crisp

* fix: Install not broken version + add update meta

* feat: Ajoute le formulaire

* fix: Corrige htmlFor et id

* feat: Ajoute la logique pour récupérer nombre de réponses et les issues

* fix: uncomment stuff

* fix: Retire log

* chore: Renomme fonction

* chore: Renomme fonction

* chore: Renomme fonction

* feat: Retire commentaires

* fix: Refacto urlParams

* chore : Nettoyage de reliquats

* fix : Ajoute TextAreaField et utilise TextField

* fix: Style issues

* fix: Améliore types

* feat: Cleaning

* feat: Ajoute variable d'env website id

* feat: Met à jour README

* wip placeholder url

* feat: Ajoute une fonction de validation du body

* feat: Ajoute validation

* chore: update yarn.lock

* fix: Add missing secret ref + cleaning
2022-10-24 15:03:14 +02:00

107 lines
2.1 KiB
TypeScript

import cors from '@koa/cors'
import Router from '@koa/router'
import 'dotenv/config'
import Koa from 'koa'
import koaBody from 'koa-body'
import {
clientId,
clientSecret,
PORT,
redirectUri,
serverUrl,
} from './config.js'
import { BodyType, sendCrispMessage } from './functions/send-crisp-message.js'
import { bree } from './jobs.js'
import { initMongodb } from './mongodb.js'
import { getAccessToken } from './oauth.js'
import { snakeToCamelCaseKeys, validateCrispBody } from './utils.js'
const mongo = await initMongodb()
type KoaState = Koa.DefaultState
type KoaContext = Koa.DefaultContext
const app = new Koa<KoaState, KoaContext>()
const router = new Router<KoaState, KoaContext>()
app.use(cors())
router.get('/connect', (ctx) => {
const { state = '' } = ctx.query
const url =
`${serverUrl}/oauth/authorize?` +
[
`client_id=${clientId}`,
`redirect_uri=${redirectUri}`,
`response_type=code`,
`state=${state?.toString()}`,
].join('&')
ctx.redirect(url)
})
router.get('/oauth', async (ctx) => {
const { code, error, state } = ctx.query
if (error) {
ctx.status = 400
ctx.body = error
return
}
if (typeof code !== 'string') {
ctx.status = 400
ctx.body = 'Bad code'
return
}
try {
const { body } = await getAccessToken({
serverUrl,
clientSecret,
clientId,
redirectUri,
code,
})
await mongo.saveOAuth(snakeToCamelCaseKeys(body))
if (
typeof state === 'string' &&
['run-all', ...bree.config.jobs.map(({ name }) => name)].includes(state)
) {
await (state === 'run-all' ? bree.run() : bree.run(state))
}
ctx.status = 200
} catch (err) {
// eslint-disable-next-line no-console
console.error(err)
ctx.status = 400
}
})
router.post('/send-crisp-message', koaBody(), async (ctx) => {
try {
const body = validateCrispBody(ctx.request.body as BodyType)
await sendCrispMessage(body)
ctx.status = 200
} catch (err) {
// eslint-disable-next-line no-console
console.error(err)
ctx.status = 400
}
})
app.use(router.routes())
app.use(router.allowedMethods())
app.listen(PORT, () => {
// eslint-disable-next-line no-console
console.log(`app listening on port ${PORT}`)
})