-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: traceable transaction, wishlist get like info (#63)
Co-authored-by: Yuwang Cai <[email protected]>
- Loading branch information
Showing
28 changed files
with
698 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,9 +4,9 @@ insert into department (acronym, name) values | |
('BIZ', 'Business School'), | ||
('ECE', 'Electrical and Computer Engineering'); | ||
|
||
insert into account (email, password_hash, password_salt, nickname, avatar_url) values | ||
('[email protected]', '$2a$10$/UwR7JKUFzyPfLLs9.DlyOXtWMLgPEob0jfF0I6sEK85t7z0vNLlK', '/+uNyqPxTo0OAecGgo3Oog==', 'Johnny', 'https://avatars.githubusercontent.com/u/78269445?v=4'), | ||
('[email protected]', '$2a$10$j4WHKfF8BRc3Vd5riEZ/8OrnU6dXIIx3vT/qf9y38lRDWy764lYQG', 'Vq3iUtK70eav3Rd0VjYFog==', 'JaneS', 'https://avatars.githubusercontent.com/u/69978374?v=4'), | ||
('[email protected]', '$2a$10$.QM6HzIPoJOllcbOdDhMxOSM6eGPs9ACV08WA.eBMEjuVAnObsqUi', 'ypJaYX91LwNjtmjjf4H8YQ==', 'AlexL', 'https://avatars.githubusercontent.com/u/13389461?v=4'), | ||
('[email protected]', '$2a$10$04CYtCiQ/.tP4wTz5rmH.ev9t2wYxfjOHx64Cp2YiaDidiIPj9MXa', 'taHXrSQ0f9HJeT/N5OgeuQ==', 'MikeB', 'https://avatars.githubusercontent.com/u/60336739?v=4'), | ||
('[email protected]', '$2a$10$OeA4WtLAccv12uylxYnqneVcAGsj0IgSerUGPXX41vRGKRlGLpf3K', 'pix8BBby+8fpK7NnUT07iA==', 'EmJ', 'https://avatars.githubusercontent.com/u/83934144?v=4'); | ||
insert into account (email, password_hash, password_salt, nickname, avatar_url) values | ||
('[email protected]', '$2a$10$/UwR7JKUFzyPfLLs9.DlyOXtWMLgPEob0jfF0I6sEK85t7z0vNLlK', '/+uNyqPxTo0OAecGgo3Oog==', 'Johnny', 'https://avatars.githubusercontent.com/u/78269445?v=4'), | ||
('[email protected]', '$2a$10$j4WHKfF8BRc3Vd5riEZ/8OrnU6dXIIx3vT/qf9y38lRDWy764lYQG', 'Vq3iUtK70eav3Rd0VjYFog==', 'JaneS', 'https://avatars.githubusercontent.com/u/69978374?v=4'), | ||
('[email protected]', '$2a$10$.QM6HzIPoJOllcbOdDhMxOSM6eGPs9ACV08WA.eBMEjuVAnObsqUi', 'ypJaYX91LwNjtmjjf4H8YQ==', 'AlexL', 'https://avatars.githubusercontent.com/u/13389461?v=4'), | ||
('[email protected]', '$2a$10$04CYtCiQ/.tP4wTz5rmH.ev9t2wYxfjOHx64Cp2YiaDidiIPj9MXa', 'taHXrSQ0f9HJeT/N5OgeuQ==', 'MikeB', 'https://avatars.githubusercontent.com/u/60336739?v=4'), | ||
('[email protected]', '$2a$10$OeA4WtLAccv12uylxYnqneVcAGsj0IgSerUGPXX41vRGKRlGLpf3K', 'pix8BBby+8fpK7NnUT07iA==', 'EmJ', 'https://avatars.githubusercontent.com/u/83934144?v=4'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,6 @@ RUN bun install --frozen-lockfile | |
|
||
COPY . . | ||
|
||
ENV NODE_ENV=test | ||
|
||
CMD ["sleep", "infinity"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,29 +14,13 @@ Each notification is guaranteed to be processed **exactly once**. | |
|
||
## Exchange | ||
|
||
| Property | Value | | ||
|-------------|--------------| | ||
| name | notification | | ||
| type | topic | | ||
| durable | true | | ||
| auto delete | false | | ||
| internal | false | | ||
| no wait | false | | ||
| args | (none) | | ||
- Name: notification | ||
- Type: topic | ||
- Durable: true | ||
|
||
## Topic | ||
## Topic: email | ||
|
||
The topic name indicates the eventual notification channel. | ||
|
||
| Topic name | Notification channel | | ||
|------------|----------------------| | ||
| email | Email | | ||
|
||
## Message | ||
|
||
It is recommended to mark all messages as `persistent`. | ||
|
||
For email topic: | ||
Send an email to one user. | ||
|
||
```json | ||
{ | ||
|
@@ -45,3 +29,23 @@ For email topic: | |
"content": "<p>Email content in HTML or plain text</p>" | ||
} | ||
``` | ||
|
||
## Topic: batch-email | ||
|
||
Send multiple emails to multiple users at once, in batch. | ||
|
||
```json | ||
{ | ||
"emails": [ | ||
{ | ||
"to": "[email protected]", | ||
"title": "Email subject 1", | ||
"content": "<p>Email content in HTML or plain text</p>" | ||
}, | ||
{ | ||
"to": "[email protected]", | ||
"title": "Email subject 2", | ||
"content": "<p>Email content in HTML or plain text</p>" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package processors | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"github.com/resend/resend-go/v2" | ||
"nshm.shop/notification/utils" | ||
) | ||
|
||
type BatchEmailPayload struct { | ||
Emails []EmailPayload `json:"emails" validate:"required,gt=0,dive,required"` | ||
} | ||
|
||
func (payload BatchEmailPayload) Process() error { | ||
err := utils.Validate(payload) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
emails := payload.Emails | ||
|
||
if os.Getenv("GO_ENV") != "production" { | ||
for _, email := range emails { | ||
log.Printf("sent emails:\n[to]\n%s\n[title]\n%s\n[content]\n%s\n", email.To, email.Title, email.Content) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
client := resend.NewClient(os.Getenv("RESEND_API_KEY")) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
sendEmailRequests := make([]*resend.SendEmailRequest, 0) | ||
for _, email := range emails { | ||
sendEmailRequests = append(sendEmailRequests, &resend.SendEmailRequest{ | ||
From: "NUS Second-Hand Market <[email protected]>", | ||
To: []string{email.To}, | ||
Subject: email.Title, | ||
Html: email.Content, | ||
}) | ||
} | ||
|
||
sent, err := client.Batch.SendWithContext(ctx, sendEmailRequests) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("sent emails: %s\n", sent.Data) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package processors | ||
|
||
import "testing" | ||
|
||
// TestProcessBatchEmailValidPayload tests Process with a valid email payload. | ||
// It should not return an error. | ||
func TestProcessBatchEmailValidPayload(t *testing.T) { | ||
payload := BatchEmailPayload{ | ||
Emails: []EmailPayload{ | ||
{ | ||
To: "[email protected]", | ||
Title: "test1", | ||
Content: "test1", | ||
}, | ||
{ | ||
To: "[email protected]", | ||
Title: "test2", | ||
Content: "test2", | ||
}, | ||
}, | ||
} | ||
|
||
err := payload.Process() | ||
if err != nil { | ||
t.Errorf("expected no error, got %v", err) | ||
} | ||
} | ||
|
||
// TestProcessBatchEmailEmptyList tests Process with an empty 'Emails' field. | ||
// It should return an error. | ||
func TestProcessBatchEmailEmptyList(t *testing.T) { | ||
payload := BatchEmailPayload{ | ||
Emails: []EmailPayload{}, | ||
} | ||
|
||
err := payload.Process() | ||
if err == nil { | ||
t.Errorf("expected error, got nil") | ||
} | ||
} | ||
|
||
// TestProcessBatchEmailEmptyTo tests Process with an empty 'To' field. | ||
// It should return an error. | ||
func TestProcessBatchEmailEmptyTo(t *testing.T) { | ||
payload := BatchEmailPayload{ | ||
Emails: []EmailPayload{ | ||
{ | ||
To: "", | ||
Title: "test", | ||
Content: "test", | ||
}, | ||
}, | ||
} | ||
|
||
err := payload.Process() | ||
if err == nil { | ||
t.Errorf("expected error, got nil") | ||
} | ||
} | ||
|
||
// TestProcessBatchEmailInvalidTo tests Process with an invalid 'To' field. | ||
// It should return an error. | ||
func TestProcessBatchEmailInvalidTo(t *testing.T) { | ||
payload := BatchEmailPayload{ | ||
Emails: []EmailPayload{ | ||
{ | ||
To: "test", | ||
Title: "test", | ||
Content: "test", | ||
}, | ||
}, | ||
} | ||
|
||
err := payload.Process() | ||
if err == nil { | ||
t.Errorf("expected error, got nil") | ||
} | ||
} | ||
|
||
// TestProcessBatchEmailEmptyTitle tests Process with an empty 'Title' field. | ||
// It should return an error. | ||
func TestProcessBatchEmailEmptyTitle(t *testing.T) { | ||
payload := BatchEmailPayload{ | ||
Emails: []EmailPayload{ | ||
{ | ||
To: "[email protected]", | ||
Title: "", | ||
Content: "test", | ||
}, | ||
}, | ||
} | ||
|
||
err := payload.Process() | ||
if err == nil { | ||
t.Errorf("expected error, got nil") | ||
} | ||
} | ||
|
||
// TestProcessBatchEmailEmptyContent tests Process with an empty 'Content' field. | ||
// It should return an error. | ||
func TestProcessBatchEmailEmptyContent(t *testing.T) { | ||
payload := BatchEmailPayload{ | ||
Emails: []EmailPayload{ | ||
{ | ||
To: "[email protected]", | ||
Title: "test", | ||
Content: "", | ||
}, | ||
}, | ||
} | ||
|
||
err := payload.Process() | ||
if err == nil { | ||
t.Errorf("expected error, got nil") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.