1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2025-09-19 05:38:48 +00:00

Get information about a group

This commit is contained in:
2019-12-15 17:37:39 +01:00
parent af9e95a914
commit 76b0d9605c
4 changed files with 197 additions and 4 deletions

View File

@@ -35,7 +35,9 @@ export interface UpdateInformation {
export interface CountQueryInformation {
table: string,
where ?: Object
where ?: Object,
customWhere ?: string,
customWhereArgs ?: Array<string>
}
export class DatabaseHelper {
@@ -319,6 +321,16 @@ export class DatabaseHelper {
sql = sql.replace("WHERE AND", "WHERE");
}
if(info.customWhere) {
if(info.where)
sql += " AND (" + info.customWhere + ")";
else
sql += "WHERE " + info.customWhere;
if(info.customWhereArgs)
info.customWhereArgs.forEach(e => args.push(e));
}
return await new Promise((r, e) => {
this.connection.query(sql, args, (err, results, f) => {
if(err){

View File

@@ -1,5 +1,5 @@
import { DatabaseHelper } from "./DatabaseHelper";
import { GroupsAccessLevel, GroupVisibilityLevel } from "../entities/Group";
import { GroupsAccessLevel, GroupVisibilityLevel, GroupInfo } from "../entities/Group";
import { GroupMembershipLevels } from "../entities/GroupMember";
/**
@@ -44,6 +44,27 @@ export class GroupsHelper {
return list.map(e => e.groups_id);
}
/**
* Get information about a group
*
* @param groupID Target group ID
* @returns Information about the group
* @throws {Error} if the group was not found
*/
public static async GetInfo(groupID: number) : Promise<GroupInfo> {
const row = await DatabaseHelper.QueryRow({
table: GROUPS_LIST_TABLE,
where: {
id: groupID.toString()
}
});
if(row == null || !row)
throw new Error("Could not get information about the group!");
return this.DbToGroupInfo(row);
}
/**
* Get the visibility level of a group
*
@@ -126,4 +147,39 @@ export class GroupsHelper {
// Especially in the case of secret groupe
return GroupsAccessLevel.NO_ACCESS;
}
/**
* Count the number of members of a group
*
* @param groupID Target group ID
*/
private static async CountMembers(groupID: number) : Promise<number> {
return await DatabaseHelper.Count({
table: GROUPS_MEMBERS_TABLE,
where: {
groups_id: groupID
},
customWhere: "level <= ?",
customWhereArgs: [GroupMembershipLevels.MEMBER.toString()]
});
}
/**
* Turn a database row into a {GroupInfo} object
*
* @param row Database entry
* @returns Generated object
*/
private static async DbToGroupInfo(row: any) : Promise<GroupInfo> {
return new GroupInfo({
id: row.id,
name: row.name,
membersCount: await this.CountMembers(row.id),
visiblity: row.visibility,
registrationLevel: row.registration_level,
postsCreationLevel: row.posts_level,
logo: (row.path_logo != null && row.path_logo && row.path_logo != "null" ? row.path_logo : undefined),
virtualDirectory: (row.virtual_directory != null && row.virtual_directory && row.virtual_directory != "null" ? row.virtual_directory : undefined)
});
}
}