From dd1d05929a67b22c45a880764dfc4fb0701ed488 Mon Sep 17 00:00:00 2001 From: dawitel Date: Tue, 1 Apr 2025 19:01:42 +0300 Subject: [PATCH] feat: models added for the notfication --- README.md | 51 ++++++++++++- db/migrations/000001_fortune.up.sql | 8 +- db/migrations/000002_notification.down.sql | 1 + db/migrations/000002_notification.up.sql | 37 +++++++++ internal/domain/notification.go | 87 ++++++++++++++++++++++ internal/services/notfication/port.go | 4 + internal/services/notfication/service.go | 11 +++ makefile | 26 +++---- 8 files changed, 207 insertions(+), 18 deletions(-) create mode 100644 db/migrations/000002_notification.down.sql create mode 100644 db/migrations/000002_notification.up.sql create mode 100644 internal/domain/notification.go create mode 100644 internal/services/notfication/port.go create mode 100644 internal/services/notfication/service.go diff --git a/README.md b/README.md index 0eadf58..8614c23 100644 --- a/README.md +++ b/README.md @@ -1 +1,50 @@ -# FortuneBet-Backend \ No newline at end of file +# FortuneBet-Backend +# Directory Structure + +├── cmd +│ ├── main.go +├── db +│ ├── migrations +│ │ ├── 000001_fortune.down.sql +│ │ ├── 000001_fortune.up.sql +│ └── query +│ ├── user.sql +├── gen +│ └── db +│ ├── db.go +│ ├── models.go +│ ├── user.sql.go +└── internal + ├── config + │ ├── config.go + ├── domain + │ ├── auth.go + │ ├── notification.go + │ ├── user.go + ├── logger + │ ├── logger.go + ├── repository + │ ├── store.go + │ ├── user.go + ├── services + │ ├── notfication + │ │ ├── port.go + │ │ ├── service.go + │ └── user + │ ├── port.go + │ ├── service.go + └── web_server + └── validator + ├── validatord.go + ├── app.go + ├── app_routes.go +├── .air.toml +├── .gitignore +├── README.md +├── compose.db.yaml +├── go.mod +├── go.sum +├── makefile +├── sqlc.yaml + +# End Directory Structure \ No newline at end of file diff --git a/db/migrations/000001_fortune.up.sql b/db/migrations/000001_fortune.up.sql index bfd54da..21db22c 100644 --- a/db/migrations/000001_fortune.up.sql +++ b/db/migrations/000001_fortune.up.sql @@ -1,4 +1,4 @@ -CREATE TABLE users ( +CREATE TABLE IF NOT EXISTS users ( id BIGSERIAL PRIMARY KEY, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, @@ -7,6 +7,6 @@ CREATE TABLE users ( password TEXT NOT NULL, role VARCHAR(50) NOT NULL, verified BOOLEAN DEFAULT FALSE, - created_at TIMESTAMP , - updated_at TIMESTAMP -); \ No newline at end of file + created_at TIMESTAMP, + updated_at TIMESTAMP +); diff --git a/db/migrations/000002_notification.down.sql b/db/migrations/000002_notification.down.sql new file mode 100644 index 0000000..45f6e69 --- /dev/null +++ b/db/migrations/000002_notification.down.sql @@ -0,0 +1 @@ +DROP TABLE notifications; diff --git a/db/migrations/000002_notification.up.sql b/db/migrations/000002_notification.up.sql new file mode 100644 index 0000000..860004e --- /dev/null +++ b/db/migrations/000002_notification.up.sql @@ -0,0 +1,37 @@ +CREATE TABLE IF NOT EXISTS notifications ( + id VARCHAR(255) PRIMARY KEY, + recipient_id VARCHAR(255) NOT NULL, + type TEXT NOT NULL CHECK ( + type IN ( + 'cash_out_success', + 'deposit_success', + 'bet_placed', + 'daily_report', + 'high_loss_on_bet', + 'bet_overload', + 'signup_welcome', + 'otp_sent' + ) + ), + level TEXT NOT NULL CHECK (level IN ('info', 'error', 'warning', 'success')), + error_severity TEXT CHECK ( + error_severity IN ('low', 'medium', 'high', 'critical', 'fatal') + ), + reciever TEXT NOT NULL CHECK (reciever IN ('admin', 'customer')), + is_read BOOLEAN NOT NULL DEFAULT FALSE, + delivery_status TEXT NOT NULL CHECK (delivery_status IN ('pending', 'sent', 'failed')), + delivery_channel TEXT CHECK ( + delivery_channel IN ('email', 'sms', 'push', 'in-app') + ), + payload JSONB NOT NULL, + priority INTEGER, + version INTEGER NOT NULL DEFAULT 0, + timestamp TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP, + metadata JSONB +); + +CREATE INDEX idx_notifications_recipient_id ON notifications (recipient_id); + +CREATE INDEX idx_notifications_timestamp ON notifications (timestamp); + +CREATE INDEX idx_notifications_type ON notifications (type); diff --git a/internal/domain/notification.go b/internal/domain/notification.go new file mode 100644 index 0000000..df2a04c --- /dev/null +++ b/internal/domain/notification.go @@ -0,0 +1,87 @@ +package domain + +import ( + "encoding/json" + "time" +) + +type NotificationType string +type NotificationRecieverSide string +type NotificationDeliveryScheme string +type NotificationLevel string +type NotificationErrorSeverity string +type NotificationDeliveryStatus string +type DeliveryChannel string + +const ( + NotificationTypeCashOutSuccess NotificationType = "cash_out_success" + NotificationTypeDepositSuccess NotificationType = "deposit_success" + NotificationTypeBetPlaced NotificationType = "bet_placed" + NotificationTypeDailyReport NotificationType = "daily_report" + NotificationTypeHighLossOnBet NotificationType = "high_loss_on_bet" + NotificationTypeBetOverload NotificationType = "bet_overload" + NotificationTypeSignUpWelcome NotificationType = "signup_welcome" + NotificationTypeOTPSent NotificationType = "otp_sent" + + NotificationRecieverSideAdmin NotificationRecieverSide = "admin" + NotificationRecieverSideCustomer NotificationRecieverSide = "customer" + + NotificationDeliverySchemeBulk NotificationDeliveryScheme = "bulk" + NotificationDeliverySchemeSingle NotificationDeliveryScheme = "single" + + NotificationLevelInfo NotificationLevel = "info" + NotificationLevelError NotificationLevel = "error" + NotificationLevelWarning NotificationLevel = "warning" + NotificationLevelSuccess NotificationLevel = "success" + + NotificationErrorSeverityLow NotificationErrorSeverity = "low" + NotificationErrorSeverityMedium NotificationErrorSeverity = "medium" + NotificationErrorSeverityHigh NotificationErrorSeverity = "high" + NotificationErrorSeverityCritical NotificationErrorSeverity = "critical" + NotificationErrorSeverityFatal NotificationErrorSeverity = "fatal" + + DeliveryStatusPending NotificationDeliveryStatus = "pending" + DeliveryStatusSent NotificationDeliveryStatus = "sent" + DeliveryStatusFailed NotificationDeliveryStatus = "failed" + + DeliveryChannelEmail DeliveryChannel = "email" + DeliveryChannelSMS DeliveryChannel = "sms" + DeliveryChannelPush DeliveryChannel = "push" + DeliveryChannelInApp DeliveryChannel = "in-app" +) + +type NotificationPayload struct { + Headline string + Message string + Tags []string +} + +type Notification struct { + ID string `json:"id"` + RecipientID string `json:"recipient_id"` + Type NotificationType `json:"type"` + Level NotificationLevel `json:"level"` + ErrorSeverity *NotificationErrorSeverity `json:"error_severity"` + Reciever NotificationRecieverSide `json:"reciever"` + IsRead bool `json:"is_read"` + DeliveryStatus NotificationDeliveryStatus `json:"delivery_status,omitempty"` + DeliveryChannel DeliveryChannel `json:"delivery_channel,omitempty"` + Payload NotificationPayload `json:"payload"` + Priority int `json:"priority,omitempty"` + Version int `json:"-"` + Timestamp time.Time `json:"timestamp"` + Metadata json.RawMessage `json:"metadata,omitempty"` +} + +func (n *Notification) ToJSON() ([]byte, error) { + return json.Marshal(n) +} + +func FromJSON(data []byte) (*Notification, error) { + var n Notification + err := json.Unmarshal(data, &n) + if err != nil { + return nil, err + } + return &n, nil +} diff --git a/internal/services/notfication/port.go b/internal/services/notfication/port.go new file mode 100644 index 0000000..af928fc --- /dev/null +++ b/internal/services/notfication/port.go @@ -0,0 +1,4 @@ +package notficationservice + +type NotificationStore interface { +} diff --git a/internal/services/notfication/service.go b/internal/services/notfication/service.go new file mode 100644 index 0000000..634cbac --- /dev/null +++ b/internal/services/notfication/service.go @@ -0,0 +1,11 @@ +package notficationservice + +type Service struct { + store NotificationStore +} + +func New(store NotificationStore) *Service { + return &Service{ + store: store, + } +} diff --git a/makefile b/makefile index 845b5e7..d22975e 100644 --- a/makefile +++ b/makefile @@ -1,32 +1,32 @@ include .env .PHONY: test test: - go test ./app + @go test ./app .PHONY: coverage coverage: - mkdir -p coverage - go test -coverprofile=coverage.out ./internal/... ; - go tool cover -func=coverage.out -o coverage/coverage.txt + @mkdir -p coverage + @go test -coverprofile=coverage.out ./internal/... + @go tool cover -func=coverage.out -o coverage/coverage.txt .PHONY: build build: - go build -ldflags="-s" -o ./bin/web ./ + @go build -ldflags="-s" -o ./bin/web ./ .PHONY: run run: - echo "Running Go application"; \ - go run ./cmd/main.go + @echo "Running Go application" + @go run ./cmd/main.go .PHONY: air air: - echo "Running air"; \ - air -c .air.toml + @echo "Running air" + @air -c .air.toml .PHONY: migrations/up migrations/new: @echo 'Creating migration files for DB_URL' - migrate create -seq -ext=.sql -dir=./db/migrations $(name) + @migrate create -seq -ext=.sql -dir=./db/migrations $(name) .PHONY: migrations/up -migrations/up: +migrations/up: @echo 'Running up migrations...' - migrate -path ./db/migrations -database $(DB_URL) up + @migrate -path ./db/migrations -database $(DB_URL) up .PHONY: swagger swagger: - swag init -g cmd/main.go + @swag init -g cmd/main.go