MCPcopy Index your code
hub / github.com/NdoleStudio/httpsms / sendUsageAlert

Method sendUsageAlert

api/pkg/services/billing_service.go:187–221  ·  view source on GitHub ↗
(ctx context.Context, userID entities.UserID)

Source from the content-addressed store, hash-verified

185}
186
187func (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
223func (service *BillingService) shouldSendAlert(user *entities.User, usage *entities.BillingUsage) bool {
224 if user.IsOnFreePlan() && (usage.TotalMessages() == 160 || usage.TotalMessages() == 180 || usage.TotalMessages() == 190) {

Callers 2

RegisterSentMessageMethod · 0.95

Calls 9

shouldSendAlertMethod · 0.95
StartWithLoggerMethod · 0.65
LoadMethod · 0.65
ErrorMethod · 0.65
WrapErrorSpanMethod · 0.65
GetCurrentMethod · 0.65
UsageLimitAlertMethod · 0.65
SendMethod · 0.65
InfoMethod · 0.65

Tested by

no test coverage detected