const hooks = require('./hooks/maintenance');

const schoolUsesLdap = params => params.school.systems.some(s => s.type === 'ldap');

class SchoolMaintenanceService {
	setup(app) {
		this.app = app;
		this.hooks(hooks);
	}

	/**
	 * GET /schools/:schoolId/maintenance
	 * Returns the current maintenance status of the given school
	 *
	 * @param {Object} params feathers request params
	 * @returns Object {schoolUsesLdap, maintenance: {active, startDate}}
	 * @memberof SchoolMaintenanceService
	 */
	async find(params) {
		return Promise.resolve({
			schoolUsesLdap: schoolUsesLdap(params),
			maintenance: {
				active: params.school.inMaintenance,
				startDate: params.school.inMaintenanceSince,
			},
		});
	}

	create(data, params) {
		return Promise.resolve({
			purpose: '(POST) nicht-ldap: ins neue Jahr setzen / ldap: transferphase starten',
			currentYear: {
				_id: '42',
				name: '2019/20',
			},
		});
	}

	update(id, data, params) {
		return Promise.resolve({
			purpose: '(UPDATE) nicht-ldap: ins neue Jahr setzen / ldap: transferphase beenden und ins neue Jahr setzen',
			currentYear: {
				_id: '42',
				name: '2019/20',
			},
		});
	}
}

module.exports = {
	SchoolMaintenanceService,
};

@@ -38,16 +38,68 @@
 nance');
+%0Aconst %7B schoolModel: School %7D = require('./model');
 %0A%0Aconst 
@@ -119,25 +119,18 @@
 p = 
-params =%3E params.
+school =%3E 
 scho
@@ -169,16 +169,259 @@
 'ldap');
+%0Aconst getStatus = school =%3E (%7B%0A%09currentYear: school.currentYear,%0A%09schoolUsesLdap: schoolUsesLdap(school),%0A%09maintenance: %7B%0A%09%09active: school.inMaintenance,%0A%09%09startDate: school.inMaintenanceSince,%0A%09%7D,%0A%7D);%0A%0Aconst determineNextYear = year =%3E year;
 %0A%0Aclass 
@@ -687,16 +687,29 @@
 Object %7B
+currentYear, 
 schoolUs
@@ -845,143 +845,357 @@
 lve(
-%7B%0A%09%09%09schoolUsesLdap: schoolUsesLdap(params),%0A%09%09%09maintenance: %7B%0A%09%09%09%09active: params.school.inMaintenance,%0A%09%09%09%09startDate: params.s
+getStatus(params.school));%0A%09%7D%0A%0A%09/**%0A%09 * POST /schools/:schoolId/maintenance%0A%09 * Enter transfer period (LDAP) or migrate school to next school year (non-LDAP)%0A%09 *%0A%09 * @param %7BObject%7D data unused%0A%09 * @param %7BObject%7D params feathers request params%0A%09 * @returns Object %7BcurrentYear, schoolUsesLdap, maintenance: %7Bactive, startDate%7D%7D%0A%09 * @memberof S
 chool
-.in
 Main
@@ -1206,31 +1206,27 @@
 nceS
+erv
 i
-n
 ce
-,
 %0A%09
-%09%09%7D,%0A%09%09%7D);%0A%09%7D%0A%0A%09
+ */%0A%09async 
 crea
@@ -1250,121 +1250,186 @@
 %7B%0A%09%09
-return Promise.resolve(%7B%0A%09%09%09purpose: '(POST) nicht-ldap: ins neue Jahr setzen / ldap: transferphase starten',%0A%09%09%09
+let %7B school %7D = params;%0A%09%09const patch = %7B%7D;%0A%09%09if (schoolUsesLdap(school)) %7B%0A%09%09%09patch.inMaintenanceSince = Date.now();%0A%09%09%7D else %7B%0A%09%09%09patch.currentYear = determineNextYear(school.
 curr
@@ -1439,57 +1439,188 @@
 Year
-: %7B
+);
 %0A%09%09%09
-%09_id: '42',%0A%09%09%09%09name: '2019/20',%0A%09%09%09%7D,%0A%09%09%7D
+patch.$unset = %7B inMaintenanceSince: '' %7D;%0A%09%09%7D%0A%09%09school = await School.findOneAndUpdate(%7B _id: school._id %7D, patch, %7B new: true %7D);%0A%09%09return Promise.resolve(getStatus(school)
 );%0A%09

Implement Create route