Delete an entities.User
(ctx context.Context, source string, userID entities.UserID)
| 288 | |
| 289 | // Delete an entities.User |
| 290 | func (service *UserService) Delete(ctx context.Context, source string, userID entities.UserID) error { |
| 291 | ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger) |
| 292 | defer span.End() |
| 293 | |
| 294 | user, err := service.repository.Load(ctx, userID) |
| 295 | if err != nil { |
| 296 | msg := fmt.Sprintf("cannot load user with ID [%s] from the [%T]", userID, service.repository) |
| 297 | return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
| 298 | } |
| 299 | |
| 300 | if !user.IsOnFreePlan() && user.SubscriptionRenewsAt != nil && user.SubscriptionRenewsAt.After(time.Now()) { |
| 301 | msg := fmt.Sprintf("cannot delete user with ID [%s] because they are have an active [%s] subscription which renews at [%s]", userID, user.SubscriptionName, user.SubscriptionRenewsAt) |
| 302 | return service.tracer.WrapErrorSpan(span, stacktrace.NewError(msg)) |
| 303 | } |
| 304 | |
| 305 | if err = service.repository.Delete(ctx, user); err != nil { |
| 306 | msg := fmt.Sprintf("could not delete user with ID [%s] from the [%T]", userID, service.repository) |
| 307 | return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
| 308 | } |
| 309 | |
| 310 | ctxLogger.Info(fmt.Sprintf("sucessfully deleted user with ID [%s] in the [%T]", userID, service.repository)) |
| 311 | |
| 312 | event, err := service.createEvent(events.UserAccountDeleted, source, &events.UserAccountDeletedPayload{ |
| 313 | UserID: userID, |
| 314 | UserEmail: user.Email, |
| 315 | Timestamp: time.Now().UTC(), |
| 316 | }) |
| 317 | if err != nil { |
| 318 | msg := fmt.Sprintf("cannot create event [%s] for user [%s]", events.UserAccountDeleted, userID) |
| 319 | return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
| 320 | } |
| 321 | |
| 322 | if err = service.dispatcher.Dispatch(ctx, event); err != nil { |
| 323 | msg := fmt.Sprintf("cannot dispatch [%s] event for user [%s]", event.Type(), userID) |
| 324 | return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg)) |
| 325 | } |
| 326 | |
| 327 | return nil |
| 328 | } |
| 329 | |
| 330 | // SendAPIKeyRotatedEmail sends an email to an entities.User when the API key is rotated |
| 331 | func (service *UserService) SendAPIKeyRotatedEmail(ctx context.Context, payload *events.UserAPIKeyRotatedPayload) error { |
nothing calls this directly
no test coverage detected