1
- import type { APIGroup } from 'groupme-api-types'
1
+ import type { APIGroup , PostGroupBody , PostGroupResponse } from 'groupme-api-types'
2
2
import type { Client } from '..'
3
3
import { BaseManager , Collection , FormerGroupManager , Group , Member , User } from '..'
4
4
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
+
5
16
type GroupsRequestParams = {
6
17
page ?: number
7
18
per_page ?: number
@@ -20,6 +31,7 @@ interface GroupManagerInterface {
20
31
client : Client
21
32
cache : Collection < string , Group >
22
33
former : FormerGroupManager
34
+ create ( options : GroupCreateOptions ) : Promise < Group >
23
35
fetch ( ) : Promise < Collection < string , Group > >
24
36
fetch ( id : string ) : Promise < Group >
25
37
fetch ( ids : string [ ] ) : Promise < Collection < string , Group | null > >
@@ -34,6 +46,42 @@ export default class GroupManager extends BaseManager<Group> implements GroupMan
34
46
this . former = new FormerGroupManager ( client )
35
47
}
36
48
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
+
37
85
/**
38
86
* Fetches groups from the API.
39
87
*
0 commit comments