Skip to content

Commit

Permalink
feature: order notification fcm (#102)
Browse files Browse the repository at this point in the history
feature: order notification fcm
  • Loading branch information
masnann authored Dec 14, 2023
2 parents 588072d + 2aadeb6 commit b0cdc68
Show file tree
Hide file tree
Showing 16 changed files with 318 additions and 69 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ jobs:
sudo docker stop ${{ secrets.CNAME }}
sudo docker rm ${{ secrets.CNAME }}
sudo docker rmi ${{ secrets.DOCKERHUB_USERNAME }}/test_disappear
sudo docker run --name ${{ secrets.CNAME }} -p 80:8000 -d -e DBHOST=${{secrets.DBHOST}} -e DBPORT=${{secrets.DBPORT}} -e DBUSER=${{secrets.DBUSER}} -e DBNAME=${{secrets.DBNAME}} -e DBPASS=${{secrets.DBPASS}} -e SECRET=${{secrets.SECRET}} -e SERVER=${{secrets.SERVER}} -e SMTP_PASS=${{secrets.SMTP_PASS}} -e SMTP_USER=${{secrets.SMTP_USER}} -e SMTP_PORT=${{secrets.SMTP_PORT}} -e CCNAME=${{secrets.CCNAME}} -e CCAPIKEY=${{secrets.CCAPIKEY}} -e CCAPISECRET=${{secrets.CCAPISECRET}} -e CCFOLDER=${{secrets.CCFOLDER}} -e OPENAIAPIKEY=${{secrets.OPENAIAPIKEY}} -e MONGOURL=${{secrets.MONGOURL}} -e CLIENTKEY=${{secrets.CLIENTKEY}} -e SERVERKEY=${{secrets.SERVERKEY}} -e REDIS_ADDR=${{secrets.REDIS_ADDR}} -e REDIS_PASS=${{secrets.REDIS_PASS}} -e RESIKEY=${{secrets.RESIKEY}} ${{ secrets.DOCKERHUB_USERNAME }}/test_disappear
sudo docker run --name ${{ secrets.CNAME }} -p 80:8000 -d -e DBHOST=${{secrets.DBHOST}} -e DBPORT=${{secrets.DBPORT}} -e DBUSER=${{secrets.DBUSER}} -e DBNAME=${{secrets.DBNAME}} -e DBPASS=${{secrets.DBPASS}} -e SECRET=${{secrets.SECRET}} -e SERVER=${{secrets.SERVER}} -e SMTP_PASS=${{secrets.SMTP_PASS}} -e SMTP_USER=${{secrets.SMTP_USER}} -e SMTP_PORT=${{secrets.SMTP_PORT}} -e CCNAME=${{secrets.CCNAME}} -e CCAPIKEY=${{secrets.CCAPIKEY}} -e CCAPISECRET=${{secrets.CCAPISECRET}} -e CCFOLDER=${{secrets.CCFOLDER}} -e OPENAIAPIKEY=${{secrets.OPENAIAPIKEY}} -e MONGOURL=${{secrets.MONGOURL}} -e CLIENTKEY=${{secrets.CLIENTKEY}} -e SERVERKEY=${{secrets.SERVERKEY}} -e REDIS_ADDR=${{secrets.REDIS_ADDR}} -e REDIS_PASS=${{secrets.REDIS_PASS}} -e RESIKEY=${{secrets.RESIKEY}} -e FIREBASEKEY=${{secrets.FIREBASEKEY}} ${{ secrets.DOCKERHUB_USERNAME }}/test_disappear
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Config struct {
ServerKey string
Redis Redis
ResiKey string
FirebaseKey string
}

type Redis struct {
Expand Down Expand Up @@ -116,6 +117,9 @@ func loadConfig() *Config {
if value, found := os.LookupEnv("RESIKEY"); found {
res.ResiKey = value
}
if value, found := os.LookupEnv("FIREBASEKEY"); found {
res.FirebaseKey = value
}

return res
}
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,17 @@ func main() {
reviewService := sReview.NewReviewService(reviewRepo, productService)
reviewHandler := hReview.NewReviewHandler(reviewService)

fcmRepo := rFcm.NewFcmRepository(db)
fcmService := sFcm.NewFcmService(fcmRepo)
fcmHandler := hFcm.NewFcmHandler(fcmService)

cartRepo := rCart.NewCartRepository(db)
cartService := sCart.NewCartService(cartRepo, productService)
cartHandler := hCart.NewCartHandler(cartService)

orderRepo := rOrder.NewOrderRepository(db, coreApi)
orderService := sOrder.NewOrderService(orderRepo, generatorID, productService,
voucherService, addressService, userService, cartService)
voucherService, addressService, userService, cartService, fcmService)
orderHandler := hOrder.NewOrderHandler(orderService)

mgodb := database.InitMongoDB(*initConfig)
Expand All @@ -141,10 +145,6 @@ func main() {
homeService := sHome.NewHomepageService(homeRepo)
homeHandler := hHome.NewHomepageHandler(homeService)

fcmRepo := rFcm.NewFcmRepository(db)
fcmService := sFcm.NewFcmService(fcmRepo)
fcmHandler := hFcm.NewFcmHandler(fcmService)

e.Pre(middleware.RemoveTrailingSlash())
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
Expand Down
15 changes: 9 additions & 6 deletions module/entities/fcm_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package entities
import "time"

type FcmModels struct {
ID uint64 `gorm:"column:id;type:BIGINT UNSIGNED;primaryKey" json:"id"`
UserID uint64 `gorm:"column:user_id;type:BIGINT UNSIGNED" json:"user_id"`
Title string `gorm:"column:title;type:varchar(255)" json:"title"`
Body string `gorm:"column:body;type:text" json:"body"`
CreatedAt time.Time `gorm:"column:created_at;type:timestamp DEFAULT CURRENT_TIMESTAMP" json:"created_at"`
DeletedAt *time.Time `gorm:"column:deleted_at;type:TIMESTAMP NULL;index" json:"deleted_at"`
ID uint64 `gorm:"column:id;type:BIGINT UNSIGNED;primaryKey" json:"id"`
OrderID string `gorm:"column:order_id;type:VARCHAR(255)" json:"order_id"`
UserID uint64 `gorm:"column:user_id;type:BIGINT UNSIGNED" json:"user_id"`
Title string `gorm:"column:title;type:varchar(255)" json:"title"`
Body string `gorm:"column:body;type:text" json:"body"`
CreatedAt time.Time `gorm:"column:created_at;type:timestamp DEFAULT CURRENT_TIMESTAMP" json:"created_at"`
DeletedAt *time.Time `gorm:"column:deleted_at;type:TIMESTAMP NULL;index" json:"deleted_at"`
User UserModels `gorm:"foreignKey:UserID" json:"user"`
Order OrderModels `gorm:"foreignKey:OrderID" json:"order"`
}

func (FcmModels) TableName() string {
Expand Down
1 change: 1 addition & 0 deletions module/entities/users_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type UserModels struct {
IsVerified bool `gorm:"column:is_verified;default:false" json:"is_verified"`
LastLogin time.Time `gorm:"column:last_login;type:timestamp;default:CURRENT_TIMESTAMP" json:"last_login"`
PreferredTopics string `gorm:"column:preferred_topics;type:TEXT" json:"preferred_topics"`
DeviceToken string `gorm:"column:device_token;type:VARCHAR(255)" json:"device_token"`
CreatedAt time.Time `gorm:"column:created_at;type:timestamp DEFAULT CURRENT_TIMESTAMP" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp DEFAULT CURRENT_TIMESTAMP" json:"updated_at"`
DeletedAt *time.Time `gorm:"column:deleted_at;type:TIMESTAMP NULL;index" json:"deleted_at"`
Expand Down
24 changes: 13 additions & 11 deletions module/feature/fcm/handler/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler

import (
"github.com/capstone-kelompok-7/backend-disappear/utils/sendnotif"
"strconv"

"github.com/capstone-kelompok-7/backend-disappear/module/entities"
Expand Down Expand Up @@ -32,30 +33,31 @@ func (h *FcmHandler) CreateFcm() echo.HandlerFunc {
return response.SendBadRequestResponse(c, "Validasi gagal: "+err.Error())
}

newFcm := &entities.FcmModels{
createFcmRequest := sendnotif.SendNotificationRequest{
UserID: fcmRequest.UserID,
Title: fcmRequest.Title,
Body: fcmRequest.Body,
Token: fcmRequest.Token,
}

res, statusFcm, err := h.service.CreateFcm(newFcm, fcmRequest.Token)
res, statusFcm, err := h.service.CreateFcm(createFcmRequest)
if err != nil {
return response.SendStatusInternalServerResponse(c, "Gagal mengirim noifikasi: "+err.Error())
return response.SendStatusInternalServerResponse(c, "Gagal mengirim notifikasi: "+err.Error())
}

return response.SendStatusCreatedResponse(c, "Berhasil mengirim noifikasi", dto.FormatFcmCreate(res, statusFcm, fcmRequest.Token))

return response.SendStatusCreatedResponse(c, "Berhasil mengirim notifikasi", dto.FormatFcmCreate(res, statusFcm, fcmRequest.Token))
}
}

func (h *FcmHandler) GetFcmByIdUser() echo.HandlerFunc {
return func(c echo.Context) error {
currentUser := c.Get("CurrentUser").(*entities.UserModels)
if currentUser.Role != "customer" {
return response.SendStatusForbiddenResponse(c, "Tidak diizinkan: Anda tidak memiliki izin")
}
var fcm []*entities.FcmModels
var err error

idUser := c.Param("id")
idUserConf, _ := strconv.Atoi(idUser)

fcm, err = h.service.GetFcmByIdUser(uint64(idUserConf))

fcm, err = h.service.GetFcmByIdUser(currentUser.ID)
if err != nil {
return response.SendStatusInternalServerResponse(c, "Gagal mendapatkan daftar notifikasi by iduser: "+err.Error())
}
Expand Down
3 changes: 2 additions & 1 deletion module/feature/fcm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fcm

import (
"github.com/capstone-kelompok-7/backend-disappear/module/entities"
"github.com/capstone-kelompok-7/backend-disappear/utils/sendnotif"
"github.com/labstack/echo/v4"
)

Expand All @@ -12,7 +13,7 @@ type RepositoryFcmInterface interface {
DeleteFcmById(id uint64) error
}
type ServiceFcmInterface interface {
CreateFcm(fcm *entities.FcmModels, token string) (*entities.FcmModels, string, error)
CreateFcm(request sendnotif.SendNotificationRequest) (*entities.FcmModels, string, error)
GetFcmByIdUser(id uint64) ([]*entities.FcmModels, error)
GetFcmById(id uint64) (*entities.FcmModels, error)
DeleteFcmById(id uint64) error
Expand Down
18 changes: 9 additions & 9 deletions module/feature/fcm/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@ func NewFcmService(repo fcm.RepositoryFcmInterface) fcm.ServiceFcmInterface {
}
}

func (s *FcmService) CreateFcm(fcm *entities.FcmModels, token string) (*entities.FcmModels, string, error) {
func (s *FcmService) CreateFcm(request sendnotif.SendNotificationRequest) (*entities.FcmModels, string, error) {
value := &entities.FcmModels{
UserID: fcm.UserID,
Title: fcm.Title,
Body: fcm.Body,
OrderID: request.OrderID,
UserID: request.UserID,
Title: request.Title,
Body: request.Body,
}
var err error
var response *entities.FcmModels
var sendsuccess string
var sendSuccess string

sendsuccess, err = sendnotif.SendNotification(fcm.Title, fcm.Body, token)
sendSuccess, err = sendnotif.SendNotification(request)
if err == nil {
response, err = s.repo.CreateFcm(value)
if err != nil {
logrus.Error("gagal membuat notification ke database")
}
} else if err != nil {
} else {
logrus.Error("gagal mengirim notification")
}

return response, sendsuccess, nil

return response, sendSuccess, err
}

func (s *FcmService) GetFcmByIdUser(id uint64) ([]*entities.FcmModels, error) {
Expand Down
20 changes: 20 additions & 0 deletions module/feature/order/dto/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,23 @@ type UpdateOrderStatus struct {
OrderStatus string `json:"order_status"`
ExtraInfo string `json:"extra_info"`
}

type SendNotificationPaymentRequest struct {
PaymentStatus string `json:"payment_status"`
OrderID string `json:"order_id"`
UserID uint64 `json:"user_id"`
UserName string `json:"user_name"`
Title string `json:"title"`
Body string `json:"body"`
Token string `json:"token"`
}

type SendNotificationOrderRequest struct {
OrderStatus string `json:"order_status"`
OrderID string `json:"order_id"`
UserID uint64 `json:"user_id"`
UserName string `json:"user_name"`
Title string `json:"title"`
Body string `json:"body"`
Token string `json:"token"`
}
Loading

0 comments on commit b0cdc68

Please sign in to comment.