-
Notifications
You must be signed in to change notification settings - Fork 14
/
group_apply.go
75 lines (57 loc) · 1.82 KB
/
group_apply.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package dao
import (
"context"
"errors"
"github.com/iimeta/iim-client/internal/consts"
"github.com/iimeta/iim-client/internal/model/do"
"github.com/iimeta/iim-client/internal/model/entity"
"github.com/iimeta/iim-client/utility/db"
"github.com/iimeta/iim-client/utility/logger"
"go.mongodb.org/mongo-driver/bson"
)
var GroupApply = NewGroupApplyDao()
type GroupApplyDao struct {
*MongoDB[entity.GroupApply]
}
func NewGroupApplyDao(database ...string) *GroupApplyDao {
if len(database) == 0 {
database = append(database, db.DefaultDatabase)
}
return &GroupApplyDao{
MongoDB: NewMongoDB[entity.GroupApply](database[0], do.GROUP_APPLY_COLLECTION),
}
}
func (d *GroupApplyDao) List(ctx context.Context, groupIds []int) ([]*entity.GroupApply, []*entity.User, error) {
groupApplyList, err := d.Find(ctx, bson.M{"group_id": bson.M{"$in": groupIds}, "status": consts.GroupApplyStatusWait}, "-updated_at")
if err != nil {
return nil, nil, err
}
userIds := make([]int, 0)
for _, apply := range groupApplyList {
userIds = append(userIds, apply.UserId)
}
userList, err := User.FindUserListByUserIds(ctx, userIds)
if err != nil {
return nil, nil, err
}
return groupApplyList, userList, nil
}
func (d *GroupApplyDao) Auth(ctx context.Context, applyId string, userId int) bool {
groupApply, err := d.FindById(ctx, applyId)
if err != nil {
logger.Error(ctx, err)
return false
}
groupMember, err := GroupMember.FindOne(ctx, bson.M{"group_id": groupApply.GroupId, "user_id": userId, "leader": bson.M{"$in": []int{1, 2}, "is_quit": 0}})
if err != nil {
logger.Error(ctx, err)
return false
}
return groupMember.Id != ""
}
func (d *GroupApplyDao) Delete(ctx context.Context, applyId string, userId int) error {
if !d.Auth(ctx, applyId, userId) {
return errors.New("auth failed")
}
return d.DeleteById(ctx, applyId)
}