refactor: redesign notification table design
This commit is contained in:
@ -222,16 +222,6 @@ func ConvertToRoleCode(r string) RoleCode {
|
||||
return RoleCodeObserver
|
||||
}
|
||||
|
||||
// GetEntityType converts integer to EntityType enum
|
||||
func GetEntityType(entityType int32) EntityType {
|
||||
switch entityType {
|
||||
case 1:
|
||||
return EntityTypeTask
|
||||
default:
|
||||
panic("Not a valid entity type!")
|
||||
}
|
||||
}
|
||||
|
||||
// GetActionType converts integer to ActionType enum
|
||||
func GetActionType(actionType int32) ActionType {
|
||||
switch actionType {
|
||||
|
@ -6,14 +6,12 @@ package graph
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jordanknott/taskcafe/internal/db"
|
||||
"github.com/jordanknott/taskcafe/internal/logger"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (r *notificationResolver) ID(ctx context.Context, obj *db.Notification) (uuid.UUID, error) {
|
||||
@ -21,12 +19,7 @@ func (r *notificationResolver) ID(ctx context.Context, obj *db.Notification) (uu
|
||||
}
|
||||
|
||||
func (r *notificationResolver) ActionType(ctx context.Context, obj *db.Notification) (ActionType, error) {
|
||||
entity, err := r.Repository.GetEntityForNotificationID(ctx, obj.NotificationID)
|
||||
if err != nil {
|
||||
return ActionTypeTaskMemberAdded, err
|
||||
}
|
||||
actionType := GetActionType(entity.ActionType)
|
||||
return actionType, nil
|
||||
return ActionTypeTaskMemberAdded, nil // TODO
|
||||
}
|
||||
|
||||
func (r *notificationResolver) CausedBy(ctx context.Context, obj *db.Notification) (*NotificationCausedBy, error) {
|
||||
@ -38,26 +31,42 @@ func (r *notificationResolver) Data(ctx context.Context, obj *db.Notification) (
|
||||
}
|
||||
|
||||
func (r *notificationResolver) CreatedAt(ctx context.Context, obj *db.Notification) (*time.Time, error) {
|
||||
entity, err := r.Repository.GetEntityForNotificationID(ctx, obj.NotificationID)
|
||||
if err != nil {
|
||||
return &time.Time{}, err
|
||||
}
|
||||
return &entity.CreatedOn, nil
|
||||
return &obj.CreatedOn, nil
|
||||
}
|
||||
|
||||
func (r *queryResolver) Notifications(ctx context.Context) ([]Notified, error) {
|
||||
userID, ok := GetUserID(ctx)
|
||||
logger.New(ctx).Info("fetching notifications")
|
||||
if !ok {
|
||||
return []db.Notification{}, errors.New("user id is missing")
|
||||
return []Notified{}, nil
|
||||
}
|
||||
notifications, err := r.Repository.GetAllNotificationsForUserID(ctx, userID)
|
||||
if err == sql.ErrNoRows {
|
||||
return []db.Notification{}, nil
|
||||
return []Notified{}, nil
|
||||
} else if err != nil {
|
||||
return []db.Notification{}, err
|
||||
return []Notified{}, err
|
||||
}
|
||||
return notifications, nil
|
||||
userNotifications := []Notified{}
|
||||
for _, notified := range notifications {
|
||||
var readAt *time.Time
|
||||
if notified.ReadAt.Valid {
|
||||
readAt = ¬ified.ReadAt.Time
|
||||
}
|
||||
n := Notified{
|
||||
ID: notified.NotifiedID,
|
||||
Read: notified.Read,
|
||||
ReadAt: readAt,
|
||||
Notification: &db.Notification{
|
||||
NotificationID: notified.NotificationID,
|
||||
CausedBy: notified.CausedBy,
|
||||
ActionType: notified.ActionType,
|
||||
Data: notified.Data,
|
||||
CreatedOn: notified.CreatedOn,
|
||||
},
|
||||
}
|
||||
userNotifications = append(userNotifications, n)
|
||||
}
|
||||
return userNotifications, nil
|
||||
}
|
||||
|
||||
func (r *subscriptionResolver) NotificationAdded(ctx context.Context) (<-chan *Notified, error) {
|
||||
@ -68,42 +77,3 @@ func (r *subscriptionResolver) NotificationAdded(ctx context.Context) (<-chan *N
|
||||
func (r *Resolver) Notification() NotificationResolver { return ¬ificationResolver{r} }
|
||||
|
||||
type notificationResolver struct{ *Resolver }
|
||||
|
||||
// !!! WARNING !!!
|
||||
// The code below was going to be deleted when updating resolvers. It has been copied here so you have
|
||||
// one last chance to move it out of harms way if you want. There are two reasons this happens:
|
||||
// - When renaming or deleting a resolver the old code will be put in here. You can safely delete
|
||||
// it when you're done.
|
||||
// - You have helper methods in this file. Move them out to keep these resolver files clean.
|
||||
func (r *notificationResolver) Entity(ctx context.Context, obj *db.Notification) (*NotificationEntity, error) {
|
||||
logger.New(ctx).WithFields(log.Fields{"notificationID": obj.NotificationID}).Info("fetching entity for notification")
|
||||
entity, err := r.Repository.GetEntityForNotificationID(ctx, obj.NotificationID)
|
||||
logger.New(ctx).WithFields(log.Fields{"entityID": entity.EntityID}).Info("fetched entity")
|
||||
if err != nil {
|
||||
return &NotificationEntity{}, err
|
||||
}
|
||||
entityType := GetEntityType(entity.EntityType)
|
||||
switch entityType {
|
||||
case EntityTypeTask:
|
||||
task, err := r.Repository.GetTaskByID(ctx, entity.EntityID)
|
||||
if err != nil {
|
||||
return &NotificationEntity{}, err
|
||||
}
|
||||
return &NotificationEntity{Type: entityType, ID: entity.EntityID, Name: task.Name}, err
|
||||
|
||||
default:
|
||||
panic(fmt.Errorf("not implemented"))
|
||||
}
|
||||
}
|
||||
func (r *notificationResolver) Actor(ctx context.Context, obj *db.Notification) (*NotificationActor, error) {
|
||||
entity, err := r.Repository.GetEntityForNotificationID(ctx, obj.NotificationID)
|
||||
if err != nil {
|
||||
return &NotificationActor{}, err
|
||||
}
|
||||
logger.New(ctx).WithFields(log.Fields{"entityID": entity.ActorID}).Info("fetching actor")
|
||||
user, err := r.Repository.GetUserAccountByID(ctx, entity.ActorID)
|
||||
if err != nil {
|
||||
return &NotificationActor{}, err
|
||||
}
|
||||
return &NotificationActor{ID: entity.ActorID, Name: user.FullName, Type: ActorTypeUser}, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user