const Syncer = require('./Syncer');
const LDAPSyncer = require('./LDAPSyncer');

/**
 * Implements syncing from multiple LDAP servers based on the Syncer interface
 * @class LDAPSystemSyncer
 * @implements {Syncer}
 */
class LDAPSystemSyncer extends Syncer {
	constructor(app, stats, logger) {
		super(app, stats, logger);
		Object.assign(this.stats, {
			systems: {},
		});
	}

	/**
     * @see {Syncer#respondsTo}
     */
	static respondsTo(target) {
		return target === 'ldap';
	}

	static params() {
		return [true];
	}

	/**
     * @see {Syncer#steps}
     */
	async steps() {
		await super.steps();
		const systems = await this.getSystems();
		const jobs = systems.map((system) => async () => {
			this.stats.systems[system.alias] = {};
			const stats = await new LDAPSyncer(this.app, this.stats.systems[system.alias], this.logger, system).sync();
			if (stats.success !== true) {
				this.stats.errors.push(`LDAP sync failed for system "${system.alias}" (${system._id}).`);
			}
			// don't let unbind errors stop the sync
			this.app.service('ldap').disconnect(system.ldapConfig)
				.catch((error) => this.logger.error('Could not unbind from LDAP server', { error }));
		});
		for (const job of jobs) {
			await job();
		}
		return [...Object.values(this.stats.systems)];
	}

	getSystems() {
		return this.app.service('systems').find({ query: { type: 'ldap', 'ldapConfig.active': true }, paginate: false })
			.then((systems) => {
				this.logInfo(`Found ${systems.length} LDAP configurations.`);
				return systems;
			});
	}
}

module.exports = LDAPSystemSyncer;

@@ -647,100 +647,39 @@
 ;%0A%09%09
+for (
 const 
-jobs = systems.map((system) =%3E async () =%3E %7B%0A%09%09%09this.stats.systems%5Bsystem.alias%5D = %7B%7D;
+system of systems) %7B
 %0A%09%09%09
@@ -727,40 +727,10 @@
 pp, 
-this.stats.systems%5Bsystem.alias%5D
+%7B%7D
 , th
@@ -888,16 +888,62 @@
 );%0A%09%09%09%7D%0A
+%09%09%09this.stats.systems%5Bsystem.alias%5D = stats;%0A%0A
 %09%09%09// do
@@ -1133,106 +1133,28 @@
 %0A%09%09%7D
-);%0A%09%09for (const job of jobs) %7B%0A%09%09%09await job();%0A%09%09%7D%0A%09%09return %5B...Object.values(this.stats.systems)%5D
+%0A%09%09return this.stats
 ;%0A%09%7D

Simplify LDAPSystemSyncer