Refactor slack messages
Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
@@ -62,5 +62,4 @@ const (
|
||||
InvitationEntityType
|
||||
MembershipEntityType
|
||||
SlackMessageEntityType
|
||||
SlackMessageUpdateEntityType
|
||||
)
|
||||
|
||||
@@ -5,17 +5,7 @@ ALTER TABLE slack_messages ADD COLUMN message_ts TEXT;
|
||||
ALTER TABLE slack_messages ADD COLUMN channel_id TEXT;
|
||||
ALTER TABLE slack_messages ADD COLUMN requester_email TEXT;
|
||||
ALTER TABLE slack_messages ADD COLUMN type slack_message_type NOT NULL;
|
||||
|
||||
ALTER TABLE slack_messages ADD CONSTRAINT unique_slack_message_org_ts_channel UNIQUE (organization_id, message_ts, channel_id);
|
||||
|
||||
CREATE TABLE slack_message_updates (
|
||||
id TEXT PRIMARY KEY,
|
||||
tenant_id TEXT NOT NULL,
|
||||
slack_message_id TEXT NOT NULL,
|
||||
body JSONB NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
sent_at TIMESTAMP WITH TIME ZONE,
|
||||
error TEXT,
|
||||
CONSTRAINT fk_slack_message_updates_slack_message_id FOREIGN KEY (slack_message_id) REFERENCES slack_messages(id) ON UPDATE CASCADE ON DELETE CASCADE
|
||||
);
|
||||
ALTER TABLE slack_messages ADD COLUMN metadata JSONB;
|
||||
ALTER TABLE slack_messages ADD COLUMN initial_slack_message_id TEXT NOT NULL;
|
||||
ALTER TABLE slack_messages ADD CONSTRAINT fk_slack_messages_initial_slack_message_id
|
||||
FOREIGN KEY (initial_slack_message_id) REFERENCES slack_messages(id) ON DELETE CASCADE;
|
||||
|
||||
@@ -28,26 +28,34 @@ import (
|
||||
|
||||
type (
|
||||
SlackMessage struct {
|
||||
ID gid.GID `db:"id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
Type SlackMessageType `db:"type"`
|
||||
Body map[string]any `db:"body"`
|
||||
MessageTS *string `db:"message_ts"`
|
||||
ChannelID *string `db:"channel_id"`
|
||||
RequesterEmail *string `db:"requester_email"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
SentAt *time.Time `db:"sent_at"`
|
||||
Error *string `db:"error"`
|
||||
ID gid.GID `db:"id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
Type SlackMessageType `db:"type"`
|
||||
Body map[string]any `db:"body"`
|
||||
MessageTS *string `db:"message_ts"`
|
||||
ChannelID *string `db:"channel_id"`
|
||||
RequesterEmail *string `db:"requester_email"`
|
||||
Metadata map[string]any `db:"metadata"`
|
||||
InitialSlackMessageID gid.GID `db:"initial_slack_message_id"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
SentAt *time.Time `db:"sent_at"`
|
||||
Error *string `db:"error"`
|
||||
}
|
||||
|
||||
ErrNoUnsentSlackMessage struct{}
|
||||
|
||||
ErrSlackMessageNotFound struct{}
|
||||
)
|
||||
|
||||
func (e ErrNoUnsentSlackMessage) Error() string {
|
||||
return "no unsent slack message found"
|
||||
}
|
||||
|
||||
func (e ErrSlackMessageNotFound) Error() string {
|
||||
return "slack message not found"
|
||||
}
|
||||
|
||||
func NewSlackMessage(
|
||||
scope Scoper,
|
||||
organizationID gid.GID,
|
||||
@@ -56,14 +64,16 @@ func NewSlackMessage(
|
||||
requesterEmail *string,
|
||||
) *SlackMessage {
|
||||
now := time.Now()
|
||||
id := gid.New(scope.GetTenantID(), SlackMessageEntityType)
|
||||
return &SlackMessage{
|
||||
ID: gid.New(scope.GetTenantID(), SlackMessageEntityType),
|
||||
OrganizationID: organizationID,
|
||||
Type: messageType,
|
||||
Body: body,
|
||||
RequesterEmail: requesterEmail,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
ID: id,
|
||||
OrganizationID: organizationID,
|
||||
Type: messageType,
|
||||
Body: body,
|
||||
RequesterEmail: requesterEmail,
|
||||
InitialSlackMessageID: id,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,19 +83,43 @@ func (s *SlackMessage) Insert(
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO slack_messages (id, tenant_id, organization_id, type, body, requester_email, created_at, updated_at)
|
||||
VALUES (@id, @tenant_id, @organization_id, @type, @body, @requester_email, @created_at, @updated_at)
|
||||
INSERT INTO slack_messages (
|
||||
id,
|
||||
tenant_id,
|
||||
organization_id,
|
||||
type,
|
||||
body,
|
||||
requester_email,
|
||||
metadata,
|
||||
initial_slack_message_id,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES (
|
||||
@id,
|
||||
@tenant_id,
|
||||
@organization_id,
|
||||
@type,
|
||||
@body,
|
||||
@requester_email,
|
||||
@metadata,
|
||||
@initial_slack_message_id,
|
||||
@created_at,
|
||||
@updated_at
|
||||
)
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": s.ID,
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"organization_id": s.OrganizationID,
|
||||
"type": s.Type,
|
||||
"body": s.Body,
|
||||
"requester_email": s.RequesterEmail,
|
||||
"created_at": s.CreatedAt,
|
||||
"updated_at": s.UpdatedAt,
|
||||
"id": s.ID,
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"organization_id": s.OrganizationID,
|
||||
"type": s.Type,
|
||||
"body": s.Body,
|
||||
"requester_email": s.RequesterEmail,
|
||||
"metadata": s.Metadata,
|
||||
"initial_slack_message_id": s.InitialSlackMessageID,
|
||||
"created_at": s.CreatedAt,
|
||||
"updated_at": s.UpdatedAt,
|
||||
}
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
@@ -101,7 +135,7 @@ func (s *SlackMessage) LoadNextUnsentForUpdate(
|
||||
conn pg.Conn,
|
||||
) error {
|
||||
q := `
|
||||
SELECT id, organization_id, type, body, message_ts, channel_id, requester_email, created_at, updated_at, sent_at, error
|
||||
SELECT id, organization_id, type, body, message_ts, channel_id, requester_email, metadata, initial_slack_message_id, created_at, updated_at, sent_at, error
|
||||
FROM slack_messages
|
||||
WHERE sent_at IS NULL AND error IS NULL
|
||||
ORDER BY created_at ASC
|
||||
@@ -128,17 +162,99 @@ FOR UPDATE
|
||||
return nil
|
||||
}
|
||||
|
||||
// This is used for Slack webhook verification where we don't know the tenant yet
|
||||
func (s *SlackMessage) LoadByChannelAndTSUnscoped(
|
||||
func (s *SlackMessage) LoadNextInitalUnsentForUpdate(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
) error {
|
||||
q := `
|
||||
SELECT id, organization_id, type, body, message_ts, channel_id, requester_email, metadata, initial_slack_message_id, created_at, updated_at, sent_at, error
|
||||
FROM slack_messages
|
||||
WHERE sent_at IS NULL AND error IS NULL AND id = initial_slack_message_id
|
||||
ORDER BY created_at ASC
|
||||
LIMIT 1
|
||||
FOR UPDATE
|
||||
`
|
||||
|
||||
rows, err := conn.Query(ctx, q)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query slack messages: %w", err)
|
||||
}
|
||||
|
||||
message, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[SlackMessage])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrNoUnsentSlackMessage{}
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot collect slack message: %w", err)
|
||||
}
|
||||
|
||||
*s = message
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SlackMessage) LoadNextUpdateUnsentForUpdate(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
sm.id,
|
||||
sm.organization_id,
|
||||
sm.type,
|
||||
sm.body,
|
||||
COALESCE(sm.message_ts, original.message_ts) as message_ts,
|
||||
COALESCE(sm.channel_id, original.channel_id) as channel_id,
|
||||
sm.requester_email,
|
||||
sm.metadata,
|
||||
sm.initial_slack_message_id,
|
||||
sm.created_at,
|
||||
sm.updated_at,
|
||||
sm.sent_at,
|
||||
sm.error
|
||||
FROM slack_messages sm
|
||||
INNER JOIN slack_messages original ON sm.initial_slack_message_id = original.id
|
||||
WHERE sm.sent_at IS NULL
|
||||
AND sm.error IS NULL
|
||||
AND sm.id != sm.initial_slack_message_id
|
||||
AND original.sent_at IS NOT NULL
|
||||
AND original.error IS NULL
|
||||
ORDER BY sm.created_at ASC
|
||||
LIMIT 1
|
||||
FOR UPDATE OF sm
|
||||
`
|
||||
|
||||
rows, err := conn.Query(ctx, q)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query slack messages: %w", err)
|
||||
}
|
||||
|
||||
message, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[SlackMessage])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrNoUnsentSlackMessage{}
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot collect slack message: %w", err)
|
||||
}
|
||||
|
||||
*s = message
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SlackMessage) LoadInitialByChannelAndTS(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
channelID string,
|
||||
messageTS string,
|
||||
) error {
|
||||
q := `
|
||||
SELECT id, organization_id, type, body, message_ts, channel_id, requester_email, created_at, updated_at, sent_at, error
|
||||
SELECT id, organization_id, type, body, message_ts, channel_id, requester_email, metadata, initial_slack_message_id, created_at, updated_at, sent_at, error
|
||||
FROM slack_messages
|
||||
WHERE message_ts = @message_ts AND channel_id = @channel_id
|
||||
WHERE message_ts = @message_ts AND channel_id = @channel_id AND id = initial_slack_message_id AND %s
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
@@ -146,6 +262,9 @@ LIMIT 1
|
||||
"message_ts": messageTS,
|
||||
"channel_id": channelID,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
@@ -168,23 +287,25 @@ LIMIT 1
|
||||
func (s *SlackMessage) Update(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
UPDATE slack_messages
|
||||
SET body = @body, sent_at = @sent_at, updated_at = @updated_at, error = @error, message_ts = @message_ts, channel_id = @channel_id
|
||||
WHERE id = @id
|
||||
SET sent_at = @sent_at, updated_at = @updated_at, error = @error
|
||||
WHERE id = @id AND %s
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": s.ID,
|
||||
"body": s.Body,
|
||||
"sent_at": s.SentAt,
|
||||
"updated_at": s.UpdatedAt,
|
||||
"error": s.Error,
|
||||
"message_ts": s.MessageTS,
|
||||
"channel_id": s.ChannelID,
|
||||
}
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot update slack message: %w", err)
|
||||
@@ -193,6 +314,40 @@ WHERE id = @id
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SlackMessage) UpdateChannelAndTSByInitialMessageID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
initialSlackMessageID gid.GID,
|
||||
channelID string,
|
||||
messageTS string,
|
||||
updatedAt time.Time,
|
||||
) error {
|
||||
q := `
|
||||
UPDATE slack_messages
|
||||
SET channel_id = @channel_id, message_ts = @message_ts, updated_at = @updated_at
|
||||
WHERE initial_slack_message_id = @initial_slack_message_id AND %s
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"initial_slack_message_id": initialSlackMessageID,
|
||||
"channel_id": channelID,
|
||||
"message_ts": messageTS,
|
||||
"updated_at": updatedAt,
|
||||
}
|
||||
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot update slack messages with initial message id: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SlackMessage) LoadById(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
@@ -200,13 +355,15 @@ func (s *SlackMessage) LoadById(
|
||||
slackMessageID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT id, organization_id, type, body, message_ts, channel_id, requester_email, created_at, updated_at, sent_at, error
|
||||
SELECT id, organization_id, type, body, message_ts, channel_id, requester_email, metadata, initial_slack_message_id, created_at, updated_at, sent_at, error
|
||||
FROM slack_messages
|
||||
WHERE id = @id
|
||||
AND %s
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": slackMessageID,
|
||||
}
|
||||
@@ -230,6 +387,46 @@ LIMIT 1
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SlackMessage) LoadLatestByInitialMessageID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
initialSlackMessageID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT id, organization_id, type, body, message_ts, channel_id, requester_email, metadata, initial_slack_message_id, created_at, updated_at, sent_at, error
|
||||
FROM slack_messages
|
||||
WHERE %s
|
||||
AND initial_slack_message_id = @initial_slack_message_id
|
||||
ORDER BY created_at DESC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"initial_slack_message_id": initialSlackMessageID,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query slack message: %w", err)
|
||||
}
|
||||
|
||||
message, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[SlackMessage])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrSlackMessageNotFound{}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
*s = message
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SlackMessage) LoadLatestByRequesterEmailAndType(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
@@ -240,7 +437,7 @@ func (s *SlackMessage) LoadLatestByRequesterEmailAndType(
|
||||
since time.Time,
|
||||
) error {
|
||||
q := `
|
||||
SELECT id, organization_id, type, body, message_ts, channel_id, requester_email, created_at, updated_at, sent_at, error
|
||||
SELECT id, organization_id, type, body, message_ts, channel_id, requester_email, metadata, initial_slack_message_id, created_at, updated_at, sent_at, error
|
||||
FROM slack_messages
|
||||
WHERE %s
|
||||
AND organization_id = @organization_id
|
||||
@@ -268,6 +465,9 @@ LIMIT 1
|
||||
|
||||
message, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[SlackMessage])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrSlackMessageNotFound{}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -1,182 +0,0 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package coredata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.gearno.de/kit/pg"
|
||||
)
|
||||
|
||||
type (
|
||||
SlackMessageUpdate struct {
|
||||
ID gid.GID `db:"id"`
|
||||
SlackMessageID gid.GID `db:"slack_message_id"`
|
||||
Body map[string]any `db:"body"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
SentAt *time.Time `db:"sent_at"`
|
||||
Error *string `db:"error"`
|
||||
}
|
||||
|
||||
ErrNoUnsentSlackMessageUpdate struct{}
|
||||
)
|
||||
|
||||
func (e ErrNoUnsentSlackMessageUpdate) Error() string {
|
||||
return "no unsent slack message update found"
|
||||
}
|
||||
|
||||
func NewSlackMessageUpdate(
|
||||
scope Scoper,
|
||||
slackMessageID gid.GID,
|
||||
body map[string]any,
|
||||
) *SlackMessageUpdate {
|
||||
now := time.Now()
|
||||
return &SlackMessageUpdate{
|
||||
ID: gid.New(scope.GetTenantID(), SlackMessageUpdateEntityType),
|
||||
SlackMessageID: slackMessageID,
|
||||
Body: body,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SlackMessageUpdate) Insert(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO slack_message_updates (id, tenant_id, slack_message_id, body, created_at, updated_at, sent_at, error)
|
||||
VALUES (@id, @tenant_id, @slack_message_id, @body, @created_at, @updated_at, @sent_at, @error)
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": s.ID,
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"slack_message_id": s.SlackMessageID,
|
||||
"body": s.Body,
|
||||
"created_at": s.CreatedAt,
|
||||
"updated_at": s.UpdatedAt,
|
||||
"sent_at": s.SentAt,
|
||||
"error": s.Error,
|
||||
}
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot insert slack message update: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SlackMessageUpdate) LoadLatestBySlackMessageID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
slackMessageID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT id, slack_message_id, body, created_at, updated_at, sent_at, error
|
||||
FROM slack_message_updates
|
||||
WHERE slack_message_id = @slack_message_id
|
||||
ORDER BY created_at DESC
|
||||
LIMIT 1
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"slack_message_id": slackMessageID,
|
||||
}
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query slack message updates: %w", err)
|
||||
}
|
||||
|
||||
update, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[SlackMessageUpdate])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*s = update
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SlackMessageUpdate) LoadNextUnsentForUpdate(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
) error {
|
||||
q := `
|
||||
SELECT smu.id, smu.slack_message_id, smu.body, smu.created_at, smu.updated_at, smu.sent_at, smu.error
|
||||
FROM slack_message_updates smu
|
||||
INNER JOIN slack_messages sm ON smu.slack_message_id = sm.id
|
||||
WHERE smu.sent_at IS NULL
|
||||
AND smu.error IS NULL
|
||||
AND sm.sent_at IS NOT NULL
|
||||
AND sm.error IS NULL
|
||||
ORDER BY smu.created_at ASC
|
||||
LIMIT 1
|
||||
FOR UPDATE OF smu
|
||||
`
|
||||
|
||||
rows, err := conn.Query(ctx, q)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query slack message updates: %w", err)
|
||||
}
|
||||
|
||||
update, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[SlackMessageUpdate])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return ErrNoUnsentSlackMessageUpdate{}
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot collect slack message update: %w", err)
|
||||
}
|
||||
|
||||
*s = update
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SlackMessageUpdate) Update(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
) error {
|
||||
q := `
|
||||
UPDATE slack_message_updates
|
||||
SET body = @body, updated_at = @updated_at, sent_at = @sent_at, error = @error
|
||||
WHERE id = @id
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"id": s.ID,
|
||||
"body": s.Body,
|
||||
"updated_at": s.UpdatedAt,
|
||||
"sent_at": s.SentAt,
|
||||
"error": s.Error,
|
||||
}
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot update slack message update: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user