Skip to content

Commit b307e11

Browse files
authored
Merge pull request #91 from striker4150/group-create
Add support for creating groups
2 parents a6d3c03 + db9b776 commit b307e11

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

src/managers/GroupManager.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
import type { APIGroup } from 'groupme-api-types'
1+
import type { APIGroup, PostGroupBody, PostGroupResponse } from 'groupme-api-types'
22
import type { Client } from '..'
33
import { BaseManager, Collection, FormerGroupManager, Group, Member, User } from '..'
44

5+
type GroupCreateOptions = {
6+
name: string
7+
type?: 'private' | 'closed'
8+
description?: string
9+
image_url?: string
10+
share?: boolean
11+
join_question?: string
12+
requires_approval?: boolean
13+
office_mode?: boolean
14+
}
15+
516
type GroupsRequestParams = {
617
page?: number
718
per_page?: number
@@ -20,6 +31,7 @@ interface GroupManagerInterface {
2031
client: Client
2132
cache: Collection<string, Group>
2233
former: FormerGroupManager
34+
create(options: GroupCreateOptions): Promise<Group>
2335
fetch(): Promise<Collection<string, Group>>
2436
fetch(id: string): Promise<Group>
2537
fetch(ids: string[]): Promise<Collection<string, Group | null>>
@@ -34,6 +46,42 @@ export default class GroupManager extends BaseManager<Group> implements GroupMan
3446
this.former = new FormerGroupManager(client)
3547
}
3648

49+
/**
50+
* Creates a group.
51+
*
52+
* @param options Options for creating a group.
53+
* @returns The created group.
54+
*/
55+
create(options: GroupCreateOptions): Promise<Group>
56+
public async create(options: GroupCreateOptions): Promise<Group> {
57+
const body: PostGroupBody = { name: options.name }
58+
if (options.type !== undefined) body.type = options.type
59+
if (options.description !== undefined) body.description = options.description
60+
if (options.image_url !== undefined) body.image_url = options.description
61+
if (options.share !== undefined) body.share = options.share
62+
if (options.join_question !== undefined) {
63+
body.show_join_question = true
64+
body.join_question = { text: options.join_question, type: 'join_reason/questions/text' }
65+
}
66+
if (options.requires_approval !== undefined) body.requires_approval = options.requires_approval
67+
if (options.office_mode !== undefined) body.office_mode = options.office_mode
68+
const res = await this.client.rest.api<PostGroupResponse>('POST', 'groups', { body })
69+
const group = this._upsert(new Group(this.client, res))
70+
if (res.members) {
71+
res.members.forEach(data => {
72+
const user = this.client.users._upsert(
73+
new User(this.client, {
74+
id: data.user_id,
75+
avatar: data.image_url,
76+
name: data.name,
77+
}),
78+
)
79+
group.members._upsert(new Member(this.client, group, user, data))
80+
})
81+
}
82+
return group
83+
}
84+
3785
/**
3886
* Fetches groups from the API.
3987
*

0 commit comments

Comments
 (0)