2023-01-06 00:39:21 +01:00
package primary
2022-11-19 23:16:54 +01:00
import (
2022-11-20 15:40:19 +01:00
scn "blackforestbytes.com/simplecloudnotifier"
2023-07-27 17:44:06 +02:00
"blackforestbytes.com/simplecloudnotifier/db"
2022-11-19 23:16:54 +01:00
"blackforestbytes.com/simplecloudnotifier/models"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
2022-12-07 23:32:58 +01:00
"gogs.mikescher.com/BlackForestBytes/goext/sq"
2022-11-19 23:16:54 +01:00
"time"
)
2023-07-27 17:44:06 +02:00
func ( db * Database ) CreateRetryDelivery ( ctx db . TxContext , client models . Client , msg models . Message ) ( models . Delivery , error ) {
2022-11-19 23:16:54 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return models . Delivery { } , err
}
2023-05-28 22:27:38 +02:00
now := time . Now ( )
2022-11-20 15:40:19 +01:00
next := scn . NextDeliveryTimestamp ( now )
2022-11-19 23:16:54 +01:00
2024-09-15 21:07:46 +02:00
entity := models . Delivery {
2023-05-28 22:27:38 +02:00
DeliveryID : models . NewDeliveryID ( ) ,
2023-01-14 00:48:51 +01:00
MessageID : msg . MessageID ,
2022-11-19 23:16:54 +01:00
ReceiverUserID : client . UserID ,
ReceiverClientID : client . ClientID ,
2024-09-15 21:07:46 +02:00
TimestampCreated : models . NewSCNTime ( now ) ,
2022-11-19 23:16:54 +01:00
TimestampFinalized : nil ,
Status : models . DeliveryStatusRetry ,
RetryCount : 0 ,
2024-09-15 21:07:46 +02:00
NextDelivery : models . NewSCNTimePtr ( & next ) ,
2022-11-19 23:16:54 +01:00
FCMMessageID : nil ,
2023-05-28 22:27:38 +02:00
}
2022-11-19 23:16:54 +01:00
2023-05-28 22:27:38 +02:00
_ , err = sq . InsertSingle ( ctx , tx , "deliveries" , entity )
2022-11-19 23:16:54 +01:00
if err != nil {
return models . Delivery { } , err
}
2024-09-15 21:07:46 +02:00
return entity , nil
2023-05-28 22:27:38 +02:00
}
2023-01-14 00:48:51 +01:00
2023-07-27 17:44:06 +02:00
func ( db * Database ) CreateSuccessDelivery ( ctx db . TxContext , client models . Client , msg models . Message , fcmDelivID string ) ( models . Delivery , error ) {
2023-05-28 22:27:38 +02:00
tx , err := ctx . GetOrCreateTransaction ( db )
2022-11-19 23:16:54 +01:00
if err != nil {
return models . Delivery { } , err
}
2023-05-28 22:27:38 +02:00
now := time . Now ( )
2024-09-15 21:07:46 +02:00
entity := models . Delivery {
2023-05-28 22:27:38 +02:00
DeliveryID : models . NewDeliveryID ( ) ,
2023-01-14 00:48:51 +01:00
MessageID : msg . MessageID ,
2022-11-19 23:16:54 +01:00
ReceiverUserID : client . UserID ,
ReceiverClientID : client . ClientID ,
2024-09-15 21:07:46 +02:00
TimestampCreated : models . NewSCNTime ( now ) ,
TimestampFinalized : models . NewSCNTimePtr ( & now ) ,
2022-11-19 23:16:54 +01:00
Status : models . DeliveryStatusSuccess ,
RetryCount : 0 ,
NextDelivery : nil ,
FCMMessageID : langext . Ptr ( fcmDelivID ) ,
2023-05-28 22:27:38 +02:00
}
_ , err = sq . InsertSingle ( ctx , tx , "deliveries" , entity )
if err != nil {
return models . Delivery { } , err
}
2024-09-15 21:07:46 +02:00
return entity , nil
2022-11-19 23:16:54 +01:00
}
2022-11-20 15:40:19 +01:00
2023-07-27 17:44:06 +02:00
func ( db * Database ) ListRetrieableDeliveries ( ctx db . TxContext , pageSize int ) ( [ ] models . Delivery , error ) {
2022-11-20 15:40:19 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return nil , err
}
2024-09-15 21:07:46 +02:00
return sq . QueryAll [ models . Delivery ] ( ctx , tx , "SELECT * FROM deliveries WHERE status = 'RETRY' AND next_delivery < :next ORDER BY next_delivery ASC LIMIT :lim" , sq . PP {
2022-12-07 22:11:44 +01:00
"next" : time2DB ( time . Now ( ) ) ,
"lim" : pageSize ,
2024-09-15 21:07:46 +02:00
} , sq . SModeExtended , sq . Safe )
2022-11-20 15:40:19 +01:00
}
2023-07-27 17:44:06 +02:00
func ( db * Database ) SetDeliverySuccess ( ctx db . TxContext , delivery models . Delivery , fcmDelivID string ) error {
2022-11-20 15:40:19 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return err
}
2022-12-07 22:11:44 +01:00
_ , err = tx . Exec ( ctx , "UPDATE deliveries SET status = 'SUCCESS', next_delivery = NULL, retry_count = :rc, timestamp_finalized = :ts, fcm_message_id = :fcm WHERE delivery_id = :did" , sq . PP {
"rc" : delivery . RetryCount + 1 ,
"ts" : time2DB ( time . Now ( ) ) ,
"fcm" : fcmDelivID ,
"did" : delivery . DeliveryID ,
} )
2022-11-20 15:40:19 +01:00
if err != nil {
return err
}
return nil
}
2023-07-27 17:44:06 +02:00
func ( db * Database ) SetDeliveryFailed ( ctx db . TxContext , delivery models . Delivery ) error {
2022-11-20 15:40:19 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return err
}
2022-12-07 22:11:44 +01:00
_ , err = tx . Exec ( ctx , "UPDATE deliveries SET status = 'FAILED', next_delivery = NULL, retry_count = :rc, timestamp_finalized = :ts WHERE delivery_id = :did" ,
sq . PP {
"rc" : delivery . RetryCount + 1 ,
"ts" : time2DB ( time . Now ( ) ) ,
"did" : delivery . DeliveryID ,
} )
2022-11-20 15:40:19 +01:00
if err != nil {
return err
}
return nil
}
2023-07-27 17:44:06 +02:00
func ( db * Database ) SetDeliveryRetry ( ctx db . TxContext , delivery models . Delivery ) error {
2022-11-20 15:40:19 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return err
}
2022-12-07 22:11:44 +01:00
_ , err = tx . Exec ( ctx , "UPDATE deliveries SET status = 'RETRY', next_delivery = :next, retry_count = :rc WHERE delivery_id = :did" , sq . PP {
"next" : scn . NextDeliveryTimestamp ( time . Now ( ) ) ,
"rc" : delivery . RetryCount + 1 ,
"did" : delivery . DeliveryID ,
} )
2022-11-20 15:40:19 +01:00
if err != nil {
return err
}
return nil
}
2023-07-27 17:44:06 +02:00
func ( db * Database ) CancelPendingDeliveries ( ctx db . TxContext , messageID models . MessageID ) error {
2022-11-20 15:40:19 +01:00
tx , err := ctx . GetOrCreateTransaction ( db )
if err != nil {
return err
}
2023-01-14 00:48:51 +01:00
_ , err = tx . Exec ( ctx , "UPDATE deliveries SET status = 'FAILED', next_delivery = NULL, timestamp_finalized = :ts WHERE message_id = :mid AND status = 'RETRY'" , sq . PP {
2022-12-07 22:11:44 +01:00
"ts" : time . Now ( ) ,
2023-01-14 00:48:51 +01:00
"mid" : messageID ,
2022-12-07 22:11:44 +01:00
} )
2022-11-20 15:40:19 +01:00
if err != nil {
return err
}
return nil
}