2021-10-26 00:42:57 +02:00
|
|
|
package graph
|
|
|
|
|
|
|
|
// This file will be automatically regenerated based on the schema, any resolver implementations
|
|
|
|
// will be copied through when generating and any unknown code will be moved to the end.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/jordanknott/taskcafe/internal/db"
|
|
|
|
"github.com/jordanknott/taskcafe/internal/logger"
|
2021-10-31 00:20:41 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-10-26 00:42:57 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (r *notificationResolver) ID(ctx context.Context, obj *db.Notification) (uuid.UUID, error) {
|
|
|
|
return obj.NotificationID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *notificationResolver) ActionType(ctx context.Context, obj *db.Notification) (ActionType, error) {
|
2021-10-27 04:29:49 +02:00
|
|
|
return ActionTypeTaskMemberAdded, nil // TODO
|
2021-10-26 00:42:57 +02:00
|
|
|
}
|
|
|
|
|
2021-10-26 21:42:04 +02:00
|
|
|
func (r *notificationResolver) CausedBy(ctx context.Context, obj *db.Notification) (*NotificationCausedBy, error) {
|
2021-10-31 00:20:41 +02:00
|
|
|
user, err := r.Repository.GetUserAccountByID(ctx, obj.CausedBy)
|
|
|
|
if err != nil {
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return &NotificationCausedBy{
|
|
|
|
Fullname: "Unknown user",
|
|
|
|
Username: "unknown",
|
|
|
|
ID: obj.CausedBy,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
log.WithError(err).Error("error while resolving Notification.CausedBy")
|
|
|
|
return &NotificationCausedBy{}, err
|
|
|
|
}
|
|
|
|
return &NotificationCausedBy{
|
|
|
|
Fullname: user.FullName,
|
|
|
|
Username: user.Username,
|
|
|
|
ID: obj.CausedBy,
|
|
|
|
}, nil
|
2021-10-26 21:42:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *notificationResolver) Data(ctx context.Context, obj *db.Notification) ([]NotificationData, error) {
|
|
|
|
panic(fmt.Errorf("not implemented"))
|
2021-10-26 00:42:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *notificationResolver) CreatedAt(ctx context.Context, obj *db.Notification) (*time.Time, error) {
|
2021-10-27 04:29:49 +02:00
|
|
|
return &obj.CreatedOn, nil
|
2021-10-26 00:42:57 +02:00
|
|
|
}
|
|
|
|
|
2021-10-26 21:42:04 +02:00
|
|
|
func (r *queryResolver) Notifications(ctx context.Context) ([]Notified, error) {
|
2021-10-26 00:42:57 +02:00
|
|
|
userID, ok := GetUserID(ctx)
|
|
|
|
logger.New(ctx).Info("fetching notifications")
|
|
|
|
if !ok {
|
2021-10-27 04:29:49 +02:00
|
|
|
return []Notified{}, nil
|
2021-10-26 00:42:57 +02:00
|
|
|
}
|
|
|
|
notifications, err := r.Repository.GetAllNotificationsForUserID(ctx, userID)
|
|
|
|
if err == sql.ErrNoRows {
|
2021-10-27 04:29:49 +02:00
|
|
|
return []Notified{}, nil
|
2021-10-26 00:42:57 +02:00
|
|
|
} else if err != nil {
|
2021-10-27 04:29:49 +02:00
|
|
|
return []Notified{}, err
|
2021-10-26 00:42:57 +02:00
|
|
|
}
|
2021-10-27 04:29:49 +02:00
|
|
|
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
|
2021-10-26 00:42:57 +02:00
|
|
|
}
|
|
|
|
|
2021-10-26 21:42:04 +02:00
|
|
|
func (r *subscriptionResolver) NotificationAdded(ctx context.Context) (<-chan *Notified, error) {
|
|
|
|
panic(fmt.Errorf("not implemented"))
|
|
|
|
}
|
|
|
|
|
2021-10-26 00:42:57 +02:00
|
|
|
// Notification returns NotificationResolver implementation.
|
|
|
|
func (r *Resolver) Notification() NotificationResolver { return ¬ificationResolver{r} }
|
|
|
|
|
|
|
|
type notificationResolver struct{ *Resolver }
|