2026-01-25 21:23:17 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"narasi-ahli-be/app/module/notifications/request"
|
|
|
|
|
"narasi-ahli-be/app/module/notifications/service"
|
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
|
|
|
|
|
|
utilRes "narasi-ahli-be/utils/response"
|
|
|
|
|
utilVal "narasi-ahli-be/utils/validator"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type notificationsController struct {
|
|
|
|
|
notificationsService service.NotificationsService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type NotificationsController interface {
|
|
|
|
|
Create(c *fiber.Ctx) error
|
|
|
|
|
ListBySentTo(c *fiber.Ctx) error
|
|
|
|
|
MarkRead(c *fiber.Ctx) error
|
|
|
|
|
Delete(c *fiber.Ctx) error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewNotificationsController(notificationsService service.NotificationsService) NotificationsController {
|
|
|
|
|
return ¬ificationsController{
|
|
|
|
|
notificationsService: notificationsService,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create Notification
|
|
|
|
|
// @Summary Create Notification
|
|
|
|
|
// @Description API untuk membuat notifikasi dan mengirim ke user berdasarkan sentTo
|
|
|
|
|
// @Tags Notifications
|
|
|
|
|
// @Security Bearer
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Param payload body request.CreateNotificationRequest true "Create Notification Payload"
|
|
|
|
|
// @Success 200 {object} response.Response
|
|
|
|
|
// @Failure 401 {object} response.Response
|
|
|
|
|
// @Failure 404 {object} response.Response
|
|
|
|
|
// @Failure 422 {object} response.Response
|
|
|
|
|
// @Failure 500 {object} response.Response
|
|
|
|
|
// @Router /notifications [post]
|
|
|
|
|
func (_i *notificationsController) Create(c *fiber.Ctx) error {
|
|
|
|
|
req := new(request.CreateNotificationRequest)
|
|
|
|
|
if err := utilVal.ParseAndValidate(c, req); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err := _i.notificationsService.Create(req.SentTo, req.SendBy, req.SendByName, req.Message)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return utilRes.Resp(c, utilRes.Response{
|
2026-01-25 21:56:12 +00:00
|
|
|
Success: true,
|
2026-01-25 21:23:17 +00:00
|
|
|
Messages: utilRes.Messages{"Notification successfully created"},
|
|
|
|
|
Data: data,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// List Notification By SentTo
|
|
|
|
|
// @Summary List Notification By SentTo
|
|
|
|
|
// @Description API untuk mengambil list notifikasi berdasarkan penerima (sentTo)
|
|
|
|
|
// @Tags Notifications
|
|
|
|
|
// @Security Bearer
|
|
|
|
|
// @Param sentTo path int true "SentTo (Receiver User ID)"
|
|
|
|
|
// @Param page query int false "Page" default(1)
|
|
|
|
|
// @Param limit query int false "Limit" default(10)
|
|
|
|
|
// @Param isRead query bool false "Filter isRead (true/false)"
|
|
|
|
|
// @Success 200 {object} response.Response
|
|
|
|
|
// @Failure 401 {object} response.Response
|
|
|
|
|
// @Failure 404 {object} response.Response
|
|
|
|
|
// @Failure 422 {object} response.Response
|
|
|
|
|
// @Failure 500 {object} response.Response
|
|
|
|
|
// @Router /notifications/{sentTo} [get]
|
|
|
|
|
func (_i *notificationsController) ListBySentTo(c *fiber.Ctx) error {
|
|
|
|
|
sentTo, err := strconv.Atoi(c.Params("sentTo"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
page, _ := strconv.Atoi(c.Query("page", "1"))
|
|
|
|
|
limit, _ := strconv.Atoi(c.Query("limit", "10"))
|
|
|
|
|
|
|
|
|
|
// optional filter isRead
|
|
|
|
|
var isRead *bool
|
|
|
|
|
if c.Query("isRead") != "" {
|
|
|
|
|
val := c.QueryBool("isRead", false)
|
|
|
|
|
isRead = &val
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, total, err := _i.notificationsService.ListBySentTo(sentTo, page, limit, isRead)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return utilRes.Resp(c, utilRes.Response{
|
2026-01-25 21:56:12 +00:00
|
|
|
Success: true,
|
2026-01-25 21:23:17 +00:00
|
|
|
Messages: utilRes.Messages{"Notifications list successfully retrieved"},
|
|
|
|
|
Data: fiber.Map{
|
|
|
|
|
"total": total,
|
|
|
|
|
"page": page,
|
|
|
|
|
"limit": limit,
|
|
|
|
|
"data": data,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Mark Read Notification
|
|
|
|
|
// @Summary Mark Read Notification
|
|
|
|
|
// @Description API untuk mengubah status notifikasi menjadi sudah dibaca
|
|
|
|
|
// @Tags Notifications
|
|
|
|
|
// @Security Bearer
|
|
|
|
|
// @Param id path int true "Notification ID"
|
|
|
|
|
// @Success 200 {object} response.Response
|
|
|
|
|
// @Failure 401 {object} response.Response
|
|
|
|
|
// @Failure 404 {object} response.Response
|
|
|
|
|
// @Failure 422 {object} response.Response
|
|
|
|
|
// @Failure 500 {object} response.Response
|
|
|
|
|
// @Router /notifications/{id}/read [put]
|
|
|
|
|
func (_i *notificationsController) MarkRead(c *fiber.Ctx) error {
|
|
|
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = _i.notificationsService.MarkRead(uint64(id))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return utilRes.Resp(c, utilRes.Response{
|
2026-01-25 21:56:12 +00:00
|
|
|
Success: true,
|
2026-01-25 21:23:17 +00:00
|
|
|
Messages: utilRes.Messages{"Notification successfully marked as read"},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Soft Delete Notification
|
|
|
|
|
// @Summary Soft Delete Notification
|
|
|
|
|
// @Description API untuk soft delete notifikasi (isActive=false)
|
|
|
|
|
// @Tags Notifications
|
|
|
|
|
// @Security Bearer
|
|
|
|
|
// @Param id path int true "Notification ID"
|
|
|
|
|
// @Success 200 {object} response.Response
|
|
|
|
|
// @Failure 401 {object} response.Response
|
|
|
|
|
// @Failure 404 {object} response.Response
|
|
|
|
|
// @Failure 422 {object} response.Response
|
|
|
|
|
// @Failure 500 {object} response.Response
|
|
|
|
|
// @Router /notifications/{id} [delete]
|
|
|
|
|
func (_i *notificationsController) Delete(c *fiber.Ctx) error {
|
|
|
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = _i.notificationsService.Delete(uint64(id))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return utilRes.Resp(c, utilRes.Response{
|
2026-01-25 21:56:12 +00:00
|
|
|
Success: true,
|
2026-01-25 21:23:17 +00:00
|
|
|
Messages: utilRes.Messages{"Notification successfully deleted"},
|
|
|
|
|
})
|
|
|
|
|
}
|