import * as express from 'express'
-import * as waterfall from 'async/waterfall'
import { database as db } from '../../../initializers/database'
-import { checkSignature, signatureValidator } from '../../../middlewares'
+import {
+ checkSignature,
+ signatureValidator,
+ setBodyHostPort,
+ remotePodsAddValidator,
+ asyncMiddleware
+} from '../../../middlewares'
+import { sendOwnedDataToPod } from '../../../lib'
+import { getMyPublicCert, getFormattedObjects } from '../../../helpers'
+import { CONFIG } from '../../../initializers'
+import { PodInstance } from '../../../models'
+import { PodSignature, Pod as FormattedPod } from '../../../../shared'
const remotePodsRouter = express.Router()
-// Post because this is a secured request
remotePodsRouter.post('/remove',
signatureValidator,
checkSignature,
- removePods
+ asyncMiddleware(removePods)
+)
+
+remotePodsRouter.post('/list',
+ asyncMiddleware(remotePodsList)
+)
+
+remotePodsRouter.post('/add',
+ setBodyHostPort, // We need to modify the host before running the validator!
+ remotePodsAddValidator,
+ asyncMiddleware(addPods)
)
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
-function removePods (req, res, next) {
- const host = req.body.signature.host
+async function addPods (req: express.Request, res: express.Response, next: express.NextFunction) {
+ const information = req.body
+
+ const pod = db.Pod.build(information)
+ const podCreated = await pod.save()
+
+ await sendOwnedDataToPod(podCreated.id)
+
+ const cert = await getMyPublicCert()
+ return res.json({ cert, email: CONFIG.ADMIN.EMAIL })
+}
+
+async function remotePodsList (req: express.Request, res: express.Response, next: express.NextFunction) {
+ const pods = await db.Pod.list()
+
+ return res.json(getFormattedObjects<FormattedPod, PodInstance>(pods, pods.length))
+}
- waterfall([
- function loadPod (callback) {
- db.Pod.loadByHost(host, callback)
- },
+async function removePods (req: express.Request, res: express.Response, next: express.NextFunction) {
+ const signature: PodSignature = req.body.signature
+ const host = signature.host
- function deletePod (pod, callback) {
- pod.destroy().asCallback(callback)
- }
- ], function (err) {
- if (err) return next(err)
+ const pod = await db.Pod.loadByHost(host)
+ await pod.destroy()
- return res.type('json').status(204).end()
- })
+ return res.type('json').status(204).end()
}