Skip to content

Commit 3cb2544

Browse files
authored
Merge pull request #70 from sireto/61-feat/server-mail-send
61 feat/server mail send
2 parents ad7abcf + d74c7db commit 3cb2544

File tree

32 files changed

+520
-108
lines changed

32 files changed

+520
-108
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ALTER TABLE servers DROP COLUMN IF EXISTS aws_credentials;
2+
3+
ALTER TABLE servers ALTER COLUMN server_type TYPE VARCHAR(255) USING server_type::TEXT;
4+
5+
DROP TYPE IF EXISTS server_type;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
DO $$
2+
DECLARE
3+
enum_exists boolean;
4+
col_exists boolean;
5+
BEGIN
6+
-- 1. Create server_type enum if not exists
7+
SELECT EXISTS (
8+
SELECT 1 FROM pg_type WHERE typname = 'server_type'
9+
) INTO enum_exists;
10+
11+
IF NOT enum_exists THEN
12+
CREATE TYPE server_type AS ENUM ('SMTP', 'AWS');
13+
END IF;
14+
15+
-- 2. Add aws_credentials column if not exists
16+
SELECT EXISTS (
17+
SELECT 1
18+
FROM information_schema.columns
19+
WHERE table_name = 'servers'
20+
AND column_name = 'aws_credentials'
21+
) INTO col_exists;
22+
23+
IF NOT col_exists THEN
24+
ALTER TABLE servers
25+
ADD COLUMN aws_credentials JSONB NOT NULL DEFAULT '{}';
26+
END IF;
27+
28+
-- 3. Convert server_type column to ENUM type safely
29+
IF EXISTS (
30+
SELECT 1
31+
FROM information_schema.columns
32+
WHERE table_name = 'servers'
33+
AND column_name = 'server_type'
34+
AND data_type = 'character varying'
35+
) THEN
36+
-- Add temporary check constraint
37+
ALTER TABLE servers
38+
ADD CONSTRAINT temp_check_server_type
39+
CHECK (server_type IN ('SMTP', 'AWS'));
40+
41+
-- Convert column type using USING clause
42+
ALTER TABLE servers
43+
ALTER COLUMN server_type TYPE server_type
44+
USING server_type::server_type;
45+
46+
-- Remove temporary constraint
47+
ALTER TABLE servers
48+
DROP CONSTRAINT temp_check_server_type;
49+
END IF;
50+
END $$;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- Down Migration: Remove "default_from_email" column from the servers table
2+
ALTER TABLE "servers"
3+
DROP COLUMN "default_from_email";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- Up Migration: Add "default_from_email" column to the servers table
2+
ALTER TABLE "servers"
3+
ADD COLUMN "default_from_email" VARCHAR NOT NULL DEFAULT '';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- Down Migration: Remove "server_id" column from the mails table
2+
ALTER TABLE "mails"
3+
DROP COLUMN "server_id";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-- Up Migration: Add "server_id" column to the mails table
2+
ALTER TABLE "mails"
3+
ADD COLUMN "server_id" UUID;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
ALTER TABLE mails
2+
ALTER COLUMN open TYPE BOOLEAN
3+
USING (
4+
CASE
5+
WHEN open IS NOT NULL THEN TRUE
6+
ELSE FALSE
7+
END
8+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
ALTER TABLE "mails"
2+
ALTER COLUMN "open" DROP DEFAULT,
3+
ALTER COLUMN "open" TYPE TIMESTAMPTZ
4+
USING (
5+
CASE
6+
WHEN open IS TRUE THEN CURRENT_TIMESTAMP
7+
ELSE NULL
8+
END
9+
),
10+
ALTER COLUMN "open" DROP NOT NULL;

backend/src/handlers/bounce_logs_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub async fn handle_sns_notification (
8080
match event_type.as_str() {
8181
"Open" => {
8282
mail_service.update_mail(mail_id, UpdateMailRequest {
83-
open: Some(true),
83+
open: Some(chrono::Utc::now()),
8484
..Default::default()
8585
}).await?;
8686
},

backend/src/handlers/mail_handler.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub async fn add_mail(
3333
campaign_id: mail.campaign_id,
3434
sent_at: mail.sent_at,
3535
status: mail.status,
36+
server_id: mail.server_id,
3637
});
3738
}
3839
Ok(Json(responses))
@@ -72,6 +73,7 @@ pub async fn get_all_mails(
7273
clicks: mail.clicks,
7374
status: mail.status.clone(),
7475
status_reason: mail.reason.clone(),
76+
server_id: mail.server_id.clone(),
7577
});
7678
});
7779
Ok(Json(responses))

0 commit comments

Comments
 (0)