Authorisation: @admin and @signedIn schema directives replace the 25 hand-copied resolver checks. A test fails if a mutation has neither and is not on the public allow-list, so a new mutation cannot ship unguarded. IDs: uint model IDs bind through graphql.UintID, removing the ten int(obj.ID) resolvers. Job status is a GraphQL enum, tied to services.statusOrder by a test. Input is strict, so the mutations no longer coerce an unknown status to "applied"; output is lenient so one legacy row cannot fail the list. deleteJobApplication and deleteJobAppReference now error on a missing id like the other deletes. The rowing reader, the job extractor and now the email classifier reach the Python service through one services.CallPython helper. The email pipeline's prompt and fence stripping moved to Python, and anthropic-sdk-go is gone from go.mod. Fix the auth test fixture, whose 11-byte secret had been panicking the services test package since InitAuth began requiring 32 bytes. Add a test that keeps the python service's compose environment list in step with the env registry. Co-Authored-By: Claude Opus 5.5 <[email protected]>
92 lines
2.6 KiB
Go
92 lines
2.6 KiB
Go
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.
|
|
// Code generated by github.com/99designs/gqlgen version v0.17.95
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"adam-french.co.uk/backend/graph/model"
|
|
"adam-french.co.uk/backend/models"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
// CreateUser is the resolver for the createUser field.
|
|
func (r *mutationResolver) CreateUser(ctx context.Context, input model.CreateUserInput) (*models.User, error) {
|
|
// Only an existing admin can create accounts (@admin in the schema) —
|
|
// there is no public sign-up. New users are created non-admin;
|
|
// setUserAdmin promotes them.
|
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(input.Password), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
user := models.User{Username: input.Username, Password: hashedPassword}
|
|
if err := r.Store.DB.Create(&user).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &user, nil
|
|
}
|
|
|
|
// DeleteUser is the resolver for the deleteUser field.
|
|
func (r *mutationResolver) DeleteUser(ctx context.Context, id int) (*models.User, error) {
|
|
var user models.User
|
|
if err := r.Store.DB.First(&user, id).Error; err != nil {
|
|
return nil, fmt.Errorf("user not found")
|
|
}
|
|
|
|
if err := r.Store.DB.Delete(&user).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &user, nil
|
|
}
|
|
|
|
// SetUserAdmin is the resolver for the setUserAdmin field.
|
|
func (r *mutationResolver) SetUserAdmin(ctx context.Context, id int, admin bool) (*models.User, error) {
|
|
callerID, ok := UserIDFromCtx(ctx)
|
|
if !ok {
|
|
return nil, fmt.Errorf("unauthorized")
|
|
}
|
|
|
|
// Guards against the last admin demoting themselves and locking everyone
|
|
// out of the admin UI.
|
|
if uint(id) == callerID {
|
|
return nil, fmt.Errorf("cannot change your own admin status")
|
|
}
|
|
|
|
var user models.User
|
|
if err := r.Store.DB.First(&user, id).Error; err != nil {
|
|
return nil, fmt.Errorf("user not found")
|
|
}
|
|
|
|
user.Admin = admin
|
|
if err := r.Store.DB.Save(&user).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &user, nil
|
|
}
|
|
|
|
// Users is the resolver for the users field.
|
|
func (r *queryResolver) Users(ctx context.Context) ([]*models.User, error) {
|
|
var users []models.User
|
|
if err := r.Store.DB.Find(&users).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return ptrs(users), nil
|
|
}
|
|
|
|
// User is the resolver for the user field.
|
|
func (r *queryResolver) User(ctx context.Context, id int) (*models.User, error) {
|
|
var user models.User
|
|
if err := r.Store.DB.First(&user, id).Error; err != nil {
|
|
return nil, fmt.Errorf("user not found")
|
|
}
|
|
return &user, nil
|
|
}
|