This refactor was born out of the inter-dependency cycles developing between the "background" module and just about every other module which was caused by the background module becoming a dependency of every module that needed to background work and the fact that the background module was also supposedly responsible for the logic for processing those tasks. Instead the "background" module is now very, very shallow and relies entirely on the Postgres NOTIFY logic for triggering jobs. There's a new table, `job` which holds just a type and single row ID. All told, this means that jobs can be added to the queue as part of the API-level or platform-level transaction, ensuring atomicity, and processing coordination is handled by the platform module, which can depend on anything.
42 lines
1.4 KiB
Go
42 lines
1.4 KiB
Go
package text
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/Gleipnir-Technology/nidus-sync/db"
|
|
"github.com/Gleipnir-Technology/nidus-sync/db/enums"
|
|
"github.com/Gleipnir-Technology/nidus-sync/platform/types"
|
|
//"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func sendReportSubscription(ctx context.Context, source, destination types.E164, content string) error {
|
|
err := EnsureInDB(ctx, db.PGInstance.BobDB, destination)
|
|
if err != nil {
|
|
return fmt.Errorf("Failed to ensure text message destination is in the DB: %w", err)
|
|
}
|
|
status, err := phoneStatus(ctx, destination)
|
|
if err != nil {
|
|
return fmt.Errorf("Failed to check if subscribed: %w", err)
|
|
}
|
|
switch status {
|
|
case enums.CommsPhonestatustypeUnconfirmed:
|
|
err = delayMessage(ctx, enums.CommsTextjobsourceRmo, destination, content, enums.CommsTextjobtypeReportConfirmation)
|
|
if err != nil {
|
|
return fmt.Errorf("Failed to delay report subscription message: %w", err)
|
|
}
|
|
err := ensureInitialText(ctx, source, destination)
|
|
if err != nil {
|
|
return fmt.Errorf("Failed to ensure initial text has been sent: %w", err)
|
|
}
|
|
return nil
|
|
case enums.CommsPhonestatustypeOkToSend:
|
|
err = sendTextBegin(ctx, source, destination, content, enums.CommsTextoriginWebsiteAction, false, true)
|
|
if err != nil {
|
|
return fmt.Errorf("Failed to send report subscription confirmation: %w", err)
|
|
}
|
|
case enums.CommsPhonestatustypeStopped:
|
|
resendInitialText(ctx, source, destination)
|
|
}
|
|
return nil
|
|
}
|