(ctx context.Context, userID entities.UserID)
| 185 | } |
| 186 | |
| 187 | func (service *BillingService) sendUsageAlert(ctx context.Context, userID entities.UserID) { |
| 188 | ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger) |
| 189 | defer span.End() |
| 190 | |
| 191 | user, err := service.userRepository.Load(ctx, userID) |
| 192 | if err != nil { |
| 193 | msg := fmt.Sprintf("cannot load user with ID [%s]", userID) |
| 194 | ctxLogger.Error(service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))) |
| 195 | return |
| 196 | } |
| 197 | |
| 198 | billingUsage, err := service.billingUsageRepository.GetCurrent(ctx, userID) |
| 199 | if err != nil { |
| 200 | msg := fmt.Sprintf("cannot load billing usage for user with ID [%s]", userID) |
| 201 | ctxLogger.Error(service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))) |
| 202 | return |
| 203 | } |
| 204 | |
| 205 | if !service.shouldSendAlert(user, billingUsage) { |
| 206 | return |
| 207 | } |
| 208 | |
| 209 | email, err := service.emailFactory.UsageLimitAlert(user, billingUsage) |
| 210 | if err != nil { |
| 211 | ctxLogger.Error(stacktrace.Propagate(err, fmt.Sprintf("cannot create usage alert email for user [%s]", user.ID))) |
| 212 | return |
| 213 | } |
| 214 | |
| 215 | if err = service.mailer.Send(ctx, email); err != nil { |
| 216 | msg := fmt.Sprintf("canot send usage alert notification to user [%s]", user.ID) |
| 217 | ctxLogger.Error(stacktrace.Propagate(err, msg)) |
| 218 | } |
| 219 | |
| 220 | ctxLogger.Info(fmt.Sprintf("usage alert email sent to user [%s]", user.ID)) |
| 221 | } |
| 222 | |
| 223 | func (service *BillingService) shouldSendAlert(user *entities.User, usage *entities.BillingUsage) bool { |
| 224 | if user.IsOnFreePlan() && (usage.TotalMessages() == 160 || usage.TotalMessages() == 180 || usage.TotalMessages() == 190) { |
no test coverage detected