90 lines
2.6 KiB
Go
90 lines
2.6 KiB
Go
package graph
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"tailly_back_v2/internal/repository"
|
|
"tailly_back_v2/internal/service"
|
|
"tailly_back_v2/proto"
|
|
)
|
|
|
|
// This file will not be regenerated automatically.
|
|
//
|
|
// It serves as dependency injection for your app, add any dependencies you require here.
|
|
|
|
type Resolver struct {
|
|
Services *service.Services
|
|
DeviceRepo repository.DeviceRepository
|
|
MessageClient proto.MessageServiceClient
|
|
SubscribeClient proto.SubscribeServiceClient
|
|
ClipClient proto.ClipServiceClient
|
|
}
|
|
|
|
func NewResolver(
|
|
services *service.Services,
|
|
db *sql.DB,
|
|
messageClient proto.MessageServiceClient,
|
|
subscribeClient proto.SubscribeServiceClient,
|
|
clipClient proto.ClipServiceClient,
|
|
) *Resolver {
|
|
return &Resolver{
|
|
Services: services,
|
|
DeviceRepo: repository.NewDeviceRepository(db),
|
|
MessageClient: messageClient,
|
|
SubscribeClient: subscribeClient,
|
|
ClipClient: clipClient,
|
|
}
|
|
}
|
|
|
|
// Mutation returns MutationResolver implementation.
|
|
func (r *Resolver) Mutation() MutationResolver { return &mutationResolver{r} }
|
|
|
|
// Query returns QueryResolver implementation.
|
|
func (r *Resolver) Query() QueryResolver { return &queryResolver{r} }
|
|
|
|
type mutationResolver struct{ *Resolver }
|
|
|
|
type queryResolver struct{ *Resolver }
|
|
|
|
// RequestEmailConfirmation - запрашивает подтверждение email
|
|
func (r *mutationResolver) RequestEmailConfirmation(ctx context.Context) (bool, error) {
|
|
userID, err := getUserIDFromContext(ctx)
|
|
if err != nil {
|
|
return false, errors.New("не авторизован")
|
|
}
|
|
|
|
user, err := r.Services.User.GetByID(ctx, userID)
|
|
if err != nil {
|
|
return false, fmt.Errorf("ошибка получения пользователя: %v", err)
|
|
}
|
|
|
|
token := user.EmailConfirmationToken
|
|
err = r.Services.Mail.SendConfirmationEmail(user.Email, token)
|
|
if err != nil {
|
|
return false, fmt.Errorf("ошибка отправки email: %v", err)
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
// ConfirmEmail - подтверждает email
|
|
func (r *mutationResolver) ConfirmEmail(ctx context.Context, token string) (bool, error) {
|
|
|
|
confirm, err := r.Services.Auth.ConfirmEmail(ctx, token)
|
|
if err != nil {
|
|
return false, fmt.Errorf("Ошибка подтверждения email: %v", err)
|
|
}
|
|
if confirm != true {
|
|
return false, fmt.Errorf("Ошибка подтверждения email: %v", err)
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
// ResendEmailConfirmation - повторно отправляет подтверждение
|
|
func (r *mutationResolver) ResendEmailConfirmation(ctx context.Context) (bool, error) {
|
|
return r.RequestEmailConfirmation(ctx)
|
|
}
|