controllers/adminaid.js

/**
 * @module controllers/adminaid
 * @author jesse reichler <mouser@donationcoder.com>
 * @copyright 5/1/19
 * @description
 * This module defines the AdminAid class, which provides support for backend administration activities
 */

"use strict";


// requirement service locator
const jrequire = require("../helpers/jrequire");

// our helper modules
const jrhMisc = require("../helpers/jrh_misc");
const JrResult = require("../helpers/jrresult");

// models
const AppModel = jrequire("models/app");
const RoomModel = jrequire("models/room");
const RoomDataModel = jrequire("models/roomdata");









/**
 * Provides support for backend administrative activities
 *
 * @class AdminAid
 */
class AdminAid {

	//---------------------------------------------------------------------------
	// constructor
	constructor() {
	}
	//---------------------------------------------------------------------------


	//---------------------------------------------------------------------------
	/**
	 * Testin helper; Just add a number of test applications, rooms for those applications, and roomdatas for those rooms
	 *
	 * @param {*} req - https request for pushign session result
	 * @param {*} user
	 * @param {*} addCountApps
	 * @param {*} addCountRooms
	 * @param {*} addCountRoomDatas
	 * @returns jrResult
	 * @memberof AdminAid
	 */
	async addTestAppsAndRooms(jrContext, user, addCountApps, addCountRooms, addCountRoomDatas) {
		// add some test apps and rooms
		let successMessage;
		let app, room, roomdata;
		let appindexString, roomindexString, roomDataIndexString;
		let appName, roomName, roomDataLabel;
		let appid, roomid;
		let appindexStart = 1;
		let roomindexStart = 1;
		const roomDataIndexStart = 1;
		let appsAdded = 0;
		let roomsAdded = 0;
		let roomDatasAdded = 0;
		let shortcode;
		const maxTrysPerClash = 1000;
		let foundone;
		let clashTryCount;

		for (let appindex = 0; appindex < addCountApps; appindex += 1) {

			// create an app
			clashTryCount = 0;
			do {
				appindexString = (appindexStart + appsAdded).toString();
				shortcode = "A" + appindexString;
				foundone = await AppModel.mFindOneByShortcode(shortcode);
				if (foundone) {
					++clashTryCount;
					++appindexStart;
				}
			} while (foundone && clashTryCount <= maxTrysPerClash);
			if (clashTryCount > maxTrysPerClash) {
				// return false;
				break;
			}

			appName = "App" + appindexString;
			app = AppModel.createModel({
				creator: user.getIdAsM(),
				shortcode,
				name: appName,
				label: "Application # " + appindexString,
				description: "Test App generated by administration backend on " + jrhMisc.getNiceNowString(),
			});

			// save app
			await app.dbSave();
			appsAdded += 1;
			appid = app.getIdAsM();

			// now create some rooms attached to the app
			for (let roomindex = 0; roomindex < addCountRooms; roomindex += 1) {

				clashTryCount = 0;
				do {
					roomindexString = (roomindexStart + roomsAdded).toString();
					shortcode = "R" + roomindexString + "A" + appindexString;
					foundone = await RoomModel.mFindOneByShortcode(shortcode);
					if (foundone) {
						++clashTryCount;
						++roomindexStart;
					}
				} while (foundone && clashTryCount <= maxTrysPerClash);
				if (clashTryCount > maxTrysPerClash) {
					// return false;
					break;
				}

				roomName = "Room" + roomindexString + "A" + appindexString;
				room = RoomModel.createModel({
					creator: user.getIdAsM(),
					appid,
					shortcode,
					name: roomName,
					label: "Room # " + roomindexString + " for App # " + appindexString,
					description: "Test Room generated by administration backend on " + jrhMisc.getNiceNowString() + " for App " + appindexString,
				});

				// save room
				await room.dbSave();
				roomsAdded += 1;
				roomid = room.getIdAsM();

				// roomdatas for each room
				for (let roomdataindex = 0; roomdataindex < addCountRoomDatas; roomdataindex += 1) {
					roomDataIndexString = (roomDataIndexStart + roomDatasAdded).toString();

					roomDataLabel = "D" + roomDataIndexString + "R" + roomindexString + "A" + appindexString;
					roomdata = RoomDataModel.createModel({
						creator: user.getIdAsM(),
						roomid,
						label: roomDataLabel,
						description: "Test RoomData generated by administration backend on " + jrhMisc.getNiceNowString() + " for Room " + roomName,
					});

					// save it
					await roomdata.dbSave();
					roomDatasAdded += 1;
				}


			}
		}

		successMessage = "Created " + appsAdded.toString() + " Apps.";
		successMessage += "\nCreated " + roomsAdded.toString() + " Rooms.";
		successMessage += "\nCreated " + roomDatasAdded.toString() + " RoomDatas.";

		// success
		jrContext.pushSuccess(successMessage);
	}
	//---------------------------------------------------------------------------


}







// export the class as the sole export
module.exports = new AdminAid();