2017-06-28 10:20:22 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const joi = require('joi');
|
2017-08-04 16:03:15 +02:00
|
|
|
const logger = require('../../logger')('/client-api/register.js');
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-12-03 08:59:13 +01:00
|
|
|
const Controller = require('../controller');
|
2018-11-29 20:46:24 +01:00
|
|
|
const { clientRegisterSchema: schema } = require('./register-schema');
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-12-03 08:59:13 +01:00
|
|
|
class RegisterController extends Controller {
|
2018-11-29 21:01:53 +01:00
|
|
|
constructor({ clientInstanceStore, clientApplicationsStore }) {
|
2018-12-03 08:59:13 +01:00
|
|
|
super();
|
2018-11-29 20:46:24 +01:00
|
|
|
this.clientInstanceStore = clientInstanceStore;
|
|
|
|
this.clientApplicationsStore = clientApplicationsStore;
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-12-03 08:59:13 +01:00
|
|
|
this.post('/', this.handleRegister);
|
2018-11-29 20:46:24 +01:00
|
|
|
}
|
2017-06-28 10:20:22 +02:00
|
|
|
|
2018-11-29 20:46:24 +01:00
|
|
|
async handleRegister(req, res) {
|
|
|
|
const data = req.body;
|
|
|
|
const { value: clientRegistration, error } = joi.validate(data, schema);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
logger.warn('Invalid client data posted', error);
|
|
|
|
return res.status(400).json(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
clientRegistration.clientIp = req.ip;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.clientApplicationsStore.upsert(clientRegistration);
|
|
|
|
await this.clientInstanceStore.insert(clientRegistration);
|
|
|
|
logger.info(
|
|
|
|
`New client registered with appName=${
|
|
|
|
clientRegistration.appName
|
|
|
|
} and instanceId=${clientRegistration.instanceId}`
|
|
|
|
);
|
|
|
|
return res.status(202).end();
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('failed to register client', err);
|
|
|
|
return res.status(500).end();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = RegisterController;
|