/
updateCalendar.js
50 lines (44 loc) · 1.42 KB
/
updateCalendar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const yup = require( 'yup' );
const CalendarClient = require( '../../calendar/CalendarClient' );
const FindOrCreateUserService = require( '../../services/FindOrCreateUserService' );
const RoomToCalendarService = require( '../../services/RoomToCalendarService' );
const UserPolicy = require( '../../policies/UserPolicy' );
const bodySchema = yup.object().shape( {
calendar: yup
.string()
.required()
.matches( /[a-zA-Z0-9]{10,64}/ ),
summary: yup
.string()
.matches( /(ROOM_.*)/ )
.required()
.max( 200 ),
name: yup
.string()
.required()
.max( 30 ),
description: yup.string().max( 50 ),
building: yup.string().max( 30 ),
floor: yup.number(),
seatsNo: yup.number(),
hasProjector: yup.bool(),
hasWhiteboard: yup.bool(),
} );
module.exports = async ( req, res ) => {
try {
const body = await bodySchema.validate( req.body );
const user = await new FindOrCreateUserService().fromRequest( req );
new UserPolicy( user ).wantsTo( 'manage rooms' );
const description = new RoomToCalendarService().descriptionFrom( body );
const calendarData = {
summary: body.summary,
description,
};
const client = new CalendarClient();
const calendar = await client.updateCalendar( body.calendar, calendarData );
res.send( calendar );
}
catch ( e ) {
res.status( 400 ).send( e.message );
}
};