mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2025-06-20 00:25:17 +00:00
Can create new groups
This commit is contained in:
@ -1,6 +1,8 @@
|
||||
import { DatabaseHelper } from "./DatabaseHelper";
|
||||
import { GroupsAccessLevel, GroupVisibilityLevel, GroupInfo } from "../entities/Group";
|
||||
import { GroupMembershipLevels, GroupMember } from "../entities/GroupMember";
|
||||
import { NewGroup } from "../entities/NewGroup";
|
||||
import { time } from "../utils/DateUtils";
|
||||
|
||||
/**
|
||||
* Groups helper
|
||||
@ -13,6 +15,37 @@ const GROUPS_MEMBERS_TABLE = "comunic_groups_members";
|
||||
|
||||
export class GroupsHelper {
|
||||
|
||||
|
||||
/**
|
||||
* Create a new group
|
||||
*
|
||||
* @param info Information about the new group to create
|
||||
* @throws {Error} In case of error
|
||||
*/
|
||||
public static async Create(info: NewGroup) : Promise<number> {
|
||||
|
||||
// Insert the group into the database
|
||||
const groupID = await DatabaseHelper.InsertRow(GROUPS_LIST_TABLE, {
|
||||
time_create: info.timeCreate,
|
||||
userid_create: info.userID,
|
||||
name: info.name
|
||||
});
|
||||
|
||||
if(groupID <= 0)
|
||||
throw Error("Could not create the group!");
|
||||
|
||||
await this.InsertMember({
|
||||
id: 0,
|
||||
groupID: groupID,
|
||||
userID: info.userID,
|
||||
timeCreate: time(),
|
||||
level: GroupMembershipLevels.ADMINISTRATOR,
|
||||
following: true
|
||||
});
|
||||
|
||||
return groupID;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check out whether a group exists or not
|
||||
*
|
||||
@ -85,6 +118,21 @@ export class GroupsHelper {
|
||||
return result.visibility;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a new group member
|
||||
*
|
||||
* @param member Information about the group member to add
|
||||
*/
|
||||
public static async InsertMember(member: GroupMember) {
|
||||
await DatabaseHelper.InsertRow(GROUPS_MEMBERS_TABLE, {
|
||||
groups_id: member.groupID,
|
||||
user_id: member.userID,
|
||||
time_create: member.timeCreate,
|
||||
level: member.level
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the membership level of a user for a group
|
||||
*
|
||||
|
Reference in New Issue
Block a user