forked from mirrors/homebox
feat: use notifiers on schedule (#362)
* fix potential memory leak with time.After * add new background service to manage scheduled notifications * update docs * remove old js reference * closes #278 * tidy
This commit is contained in:
parent
975e636fb6
commit
840d220d4f
16 changed files with 201 additions and 52 deletions
|
@ -16,7 +16,36 @@ import (
|
|||
)
|
||||
|
||||
type GroupRepository struct {
|
||||
db *ent.Client
|
||||
db *ent.Client
|
||||
groupMapper MapFunc[*ent.Group, Group]
|
||||
invitationMapper MapFunc[*ent.GroupInvitationToken, GroupInvitation]
|
||||
}
|
||||
|
||||
func NewGroupRepository(db *ent.Client) *GroupRepository {
|
||||
gmap := func(g *ent.Group) Group {
|
||||
return Group{
|
||||
ID: g.ID,
|
||||
Name: g.Name,
|
||||
CreatedAt: g.CreatedAt,
|
||||
UpdatedAt: g.UpdatedAt,
|
||||
Currency: strings.ToUpper(g.Currency.String()),
|
||||
}
|
||||
}
|
||||
|
||||
imap := func(i *ent.GroupInvitationToken) GroupInvitation {
|
||||
return GroupInvitation{
|
||||
ID: i.ID,
|
||||
ExpiresAt: i.ExpiresAt,
|
||||
Uses: i.Uses,
|
||||
Group: gmap(i.Edges.Group),
|
||||
}
|
||||
}
|
||||
|
||||
return &GroupRepository{
|
||||
db: db,
|
||||
groupMapper: gmap,
|
||||
invitationMapper: imap,
|
||||
}
|
||||
}
|
||||
|
||||
type (
|
||||
|
@ -76,27 +105,8 @@ type (
|
|||
}
|
||||
)
|
||||
|
||||
var mapToGroupErr = mapTErrFunc(mapToGroup)
|
||||
|
||||
func mapToGroup(g *ent.Group) Group {
|
||||
return Group{
|
||||
ID: g.ID,
|
||||
Name: g.Name,
|
||||
CreatedAt: g.CreatedAt,
|
||||
UpdatedAt: g.UpdatedAt,
|
||||
Currency: strings.ToUpper(g.Currency.String()),
|
||||
}
|
||||
}
|
||||
|
||||
var mapToGroupInvitationErr = mapTErrFunc(mapToGroupInvitation)
|
||||
|
||||
func mapToGroupInvitation(g *ent.GroupInvitationToken) GroupInvitation {
|
||||
return GroupInvitation{
|
||||
ID: g.ID,
|
||||
ExpiresAt: g.ExpiresAt,
|
||||
Uses: g.Uses,
|
||||
Group: mapToGroup(g.Edges.Group),
|
||||
}
|
||||
func (r *GroupRepository) GetAllGroups(ctx context.Context) ([]Group, error) {
|
||||
return r.groupMapper.MapEachErr(r.db.Group.Query().All(ctx))
|
||||
}
|
||||
|
||||
func (r *GroupRepository) StatsLocationsByPurchasePrice(ctx context.Context, GID uuid.UUID) ([]TotalsByOrganizer, error) {
|
||||
|
@ -249,7 +259,7 @@ func (r *GroupRepository) StatsGroup(ctx context.Context, GID uuid.UUID) (GroupS
|
|||
}
|
||||
|
||||
func (r *GroupRepository) GroupCreate(ctx context.Context, name string) (Group, error) {
|
||||
return mapToGroupErr(r.db.Group.Create().
|
||||
return r.groupMapper.MapErr(r.db.Group.Create().
|
||||
SetName(name).
|
||||
Save(ctx))
|
||||
}
|
||||
|
@ -262,15 +272,15 @@ func (r *GroupRepository) GroupUpdate(ctx context.Context, ID uuid.UUID, data Gr
|
|||
SetCurrency(currency).
|
||||
Save(ctx)
|
||||
|
||||
return mapToGroupErr(entity, err)
|
||||
return r.groupMapper.MapErr(entity, err)
|
||||
}
|
||||
|
||||
func (r *GroupRepository) GroupByID(ctx context.Context, id uuid.UUID) (Group, error) {
|
||||
return mapToGroupErr(r.db.Group.Get(ctx, id))
|
||||
return r.groupMapper.MapErr(r.db.Group.Get(ctx, id))
|
||||
}
|
||||
|
||||
func (r *GroupRepository) InvitationGet(ctx context.Context, token []byte) (GroupInvitation, error) {
|
||||
return mapToGroupInvitationErr(r.db.GroupInvitationToken.Query().
|
||||
return r.invitationMapper.MapErr(r.db.GroupInvitationToken.Query().
|
||||
Where(groupinvitationtoken.Token(token)).
|
||||
WithGroup().
|
||||
Only(ctx))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue