Untitled

Anonymous
plain_text
02/24/2026 7:51 AM
7.7 KB
8
Indexable
import HTTP from '../../../../utils/http.js';
import { Insert as InsertUserServiceOffered, Update as UpdateUserServiceOffered} from './services-offered.js';

export class List extends HTTP {

	async execute({user_id = null}) {

		let userId = this.user.id;

		if(user_id) {

			this.user.privileges.needs('user_manager');
			userId = user_id;
		}

		return this.mysql.query(
			`SELECT
				ur.id,
				ur.role_id,
				cr.name,
				ur.is_primary
			FROM
				user_role ur
			JOIN
				case_role cr
				ON
				ur.role_id = cr.id AND
				cr.is_deleted = 0
			WHERE
				ur.account_id = ? AND
				ur.user_id = ? AND
				ur.is_deleted = 0`,
			[ this.account.id, userId]
		)
	}
}

export class Insert extends HTTP {

	async execute({role_id, user_id = null}, skip_privilege = false) {

		let userId = this.user.id;

		if(skip_privilege) {

			userId = user_id;

		} else if(user_id) {

			this.user.privileges.needs('user_manager');
			userId = user_id;
		}

		const [userRole] = await this.mysql.query(
			`SELECT 
				id 
			FROM 
				user_role 
			WHERE 
				user_id = ? AND 
				account_id = ? AND 
				is_deleted = 0 and 
				role_id = ?`,
			[userId, this.account.id, role_id]
		);

		if(userRole) {
			throw new HTTP.Exception('Role Already Exist',400)
		}

		const data = {
			account_id: this.account.id,
			user_id: userId,
			role_id: role_id,
			created_by: this.user.id
		}

		await this.mysql.query(`
			INSERT INTO user_role SET ?
			ON DUPLICATE KEY UPDATE
			is_deleted = 0
			`,
			[data], 'write'
		);

		return await this.mysql.query(
			`SELECT 
				* 
			FROM 
				user_role 
			WHERE 
				user_id = ? AND 
				is_deleted = 0 AND 
				account_id = ?`,
			[userId, this.account.id]
		);
	}
}

export class Primary extends HTTP {

	async execute({id, role_id, user_id = null}, skip_privilege = false) {

		let userId = this.user.id;

		if(skip_privilege) {

			userId = user_id;

		} else if(user_id) {

			this.user.privileges.needs('user_manager');
			userId = user_id;
		}

		const [userRole] = await this.mysql.query(
			`SELECT
				id
			FROM
				user_role
			WHERE
				id = ? AND
				account_id = ? AND
				user_id = ? AND
				role_id = ?`,
			[id, this.account.id, userId, role_id]
		);

		if(!userRole) {
			throw new HTTP.Exception('Invalid Id', 400);
		}

		await this.mysql.query(
			`UPDATE 
				user_role 
			SET 
				\`is_primary\` = 0 
			WHERE
				account_id = ? AND 
				user_id = ? AND
				\`is_primary\` = 1`,
			[this.account.id, userId],
			'write'
		);

		await this.mysql.query(
			`UPDATE 
				user_role 
			SET 
				\`is_primary\` = 1 
			WHERE 
				id = ? and 
				account_id = ?`,
			[userRole.id, this.account.id],
			'write'
		);

		const userRoles =  await this.mysql.query(
			`SELECT 
				* 
			FROM 
				user_role 
			WHERE 
				user_id = ? AND 
				is_deleted = 0 AND 
				account_id = ?`,
			[userId, this.account.id]
		);

		let currentUserRole;

		for(const userRole of userRoles) {

			if(userRole.role_id == role_id) {
				currentUserRole = userRole;
			}
		}

		if(currentUserRole.is_primary) {

			const [mappedServiceOffered] = await this.mysql.query(
				`SELECT
					*
				FROM
					service_offered
				WHERE
					performer = ? AND
					account_id = ? AND
					is_deleted = 0`,
				[role_id, this.account.id]
			);

			if(mappedServiceOffered) {

				// Unset any existing primary service offered for this user first
				await this.mysql.query(
					`UPDATE 
						user_service_offered 
					SET 
						is_primary = 0 
					WHERE 
						user_id = ? AND 
						account_id = ? AND 
						is_primary = 1 AND
						is_deleted = 0`,
					[userId, this.account.id],
					'write'
				);

				const [userServiceOfferedExists] = await this.mysql.query(
					`SELECT
						*
					FROM
						user_service_offered
					WHERE
						user_id = ? AND
						service_offered_id = ? AND
						account_id = ? AND
						is_deleted = 0`,
					[userId, mappedServiceOffered.id, this.account.id]
				);

				if(!userServiceOfferedExists) {

					const insertUserServiceOffered = new InsertUserServiceOffered(this.request, this.response);
					await insertUserServiceOffered.initiate();
					await insertUserServiceOffered.execute({
						user_id: userId,
						service_offered_id: mappedServiceOffered.id,
						is_primary: 1
					}, true);
				} else if(userServiceOfferedExists && !userServiceOfferedExists.primary) {

					const updatetUserServiceOffered = new UpdateUserServiceOffered(this.request, this.response);
					await updatetUserServiceOffered.initiate();
					await updatetUserServiceOffered.execute({
						id: userServiceOfferedExists.id,
						user_id: userId,
						service_offered_id: mappedServiceOffered.id,
						is_primary: 1
					});
				}
			}
		}

		return userRoles;
	}
}

export class Delete extends HTTP {

	async execute({id, role_id, user_id = null}) {

		let userId = this.user.id;

		if(user_id) {

			this.user.privileges.needs('user_manager');
			userId = user_id;
		}

		// Fetch role with is_primary info
		const [userRole] = await this.mysql.query(
			`SELECT 
				id,
				is_primary
			FROM 
				user_role 
			WHERE 
				user_id = ? AND 
				id = ? AND 
				account_id = ? AND 
				is_deleted = 0 AND 
				role_id = ?`,
			[userId, id, this.account.id, role_id]
		);

		if(!userRole) {
			throw new HTTP.Exception('Invalid Id', 400);
		}

		// Prevent deletion of primary role
		if(userRole.is_primary) {
			throw new HTTP.Exception('Cannot delete primary role. Please set another role as primary first.', 400);
		}

		await this.mysql.query(
			`UPDATE 
				user_role 
			SET 
				is_deleted = 1 
			WHERE 
				user_id = ? AND 
				id = ? AND 
				account_id = ? AND 
				is_deleted = 0 AND 
				role_id = ?`,
			[userId, id, this.account.id, role_id],
			'write'
		);

		return await this.mysql.query(
			`SELECT 
				* 
			FROM 
				user_role 
			WHERE 
				user_id = ? AND 
				is_deleted = 0 AND
				account_id = ?`,
			[userId, this.account.id]
		);
	}
}
Editor is loading...
Leave a Comment