feat: new features
This commit is contained in:
@@ -36,6 +36,7 @@ func main() {
|
||||
userRepo := repositories.NewUserRepository(postgres.Pool)
|
||||
oauthRepo := repositories.NewOAuthRepository(postgres.Pool)
|
||||
positionRepo := repositories.NewPositionRepository(postgres.Pool)
|
||||
citizenshipRepo := repositories.NewCitizenshipRepository(postgres.Pool)
|
||||
|
||||
if err := seedClient(context.Background(), oauthRepo, cfg.Client); err != nil {
|
||||
log.Fatalf("seed client: %v", err)
|
||||
@@ -58,8 +59,10 @@ func main() {
|
||||
oauthHandler := handlers.NewOAuthHandler(svc, cfg.Server.FrontendURL)
|
||||
consentHandler := handlers.NewConsentHandler(svc)
|
||||
positionsHandler := handlers.NewPositionsHandler(svc)
|
||||
citizenshipSvc := services.NewCitizenshipService(citizenshipRepo)
|
||||
citizenshipHandler := handlers.NewCitizenshipHandler(citizenshipSvc)
|
||||
|
||||
router := http.NewRouter(authHandler, oauthHandler, consentHandler, positionsHandler, svc, oauthRepo, limiter)
|
||||
router := http.NewRouter(authHandler, oauthHandler, consentHandler, positionsHandler, citizenshipHandler, svc, oauthRepo, limiter)
|
||||
|
||||
log.Printf("starting server on %s", cfg.Server.Addr)
|
||||
if err := router.Run(cfg.Server.Addr); err != nil {
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/dusiburg/kotosoft-id/internal/models"
|
||||
"github.com/dusiburg/kotosoft-id/internal/services"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type CitizenshipHandler struct {
|
||||
s *services.CitizenshipService
|
||||
}
|
||||
|
||||
func NewCitizenshipHandler(s *services.CitizenshipService) *CitizenshipHandler {
|
||||
return &CitizenshipHandler{s: s}
|
||||
}
|
||||
|
||||
func (h *CitizenshipHandler) GetCitizenshipApplicationByUserId(c *gin.Context) {
|
||||
userID := mustUserID(c)
|
||||
|
||||
application, err := h.s.GetCitizenshipApplicationByUserId(c.Request.Context(), userID)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, application)
|
||||
}
|
||||
|
||||
func (h *CitizenshipHandler) CreateCitizenshipApplication(c *gin.Context) {
|
||||
userID := mustUserID(c)
|
||||
|
||||
var input models.CreateCitizenshipApplicationInput
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.Error(models.ErrBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
application, err := h.s.CreateCitizenshipApplication(c.Request.Context(), userID, input.RegionID)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, application)
|
||||
}
|
||||
|
||||
func (h *CitizenshipHandler) ReviewCitizenshipApplication(c *gin.Context) {
|
||||
reviewerID := mustUserID(c)
|
||||
|
||||
applicationID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
c.Error(models.ErrBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var input models.ReviewCitizenshipApplicationInput
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.Error(models.ErrBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
application, err := h.s.ReviewCitizenshipApplication(c.Request.Context(), applicationID, reviewerID, input)
|
||||
if err != nil {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, application)
|
||||
}
|
||||
@@ -17,6 +17,7 @@ func NewRouter(
|
||||
oauth *handlers.OAuthHandler,
|
||||
consents *handlers.ConsentHandler,
|
||||
positions *handlers.PositionsHandler,
|
||||
citizenship *handlers.CitizenshipHandler,
|
||||
svc *services.Service,
|
||||
repo *repositories.OAuthRepository,
|
||||
limiter *services.RateLimiter,
|
||||
@@ -57,9 +58,13 @@ func NewRouter(
|
||||
me.GET("/consents", authMiddleware(svc, repo, "profile:consent:read"), consents.GetUserConsents)
|
||||
me.DELETE("/consent/:clientId", authMiddleware(svc, repo, "profile:consent:delete"), consents.RevokeAppAccess)
|
||||
me.GET("/positions", authMiddleware(svc, repo, "profile:read"), positions.GetMyPositions)
|
||||
me.GET("/citizenship", authMiddleware(svc, repo, "profile:read"), citizenship.GetCitizenshipApplicationByUserId)
|
||||
me.POST("/citizenship", authMiddleware(svc, repo, "profile:edit"), citizenship.CreateCitizenshipApplication)
|
||||
|
||||
admin := api.Group("/admin", authMiddleware(svc, repo))
|
||||
{
|
||||
admin.PATCH("/citizenship/applications/:id", middleware.AdminOnly(), citizenship.ReviewCitizenshipApplication)
|
||||
|
||||
manage := admin.Group("", permissionMiddleware(svc, "positions.manage"))
|
||||
{
|
||||
manage.GET("/permissions", positions.ListPermissions)
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type CitizenshipApplicationStatus string
|
||||
|
||||
const (
|
||||
StatusPending CitizenshipApplicationStatus = "pending"
|
||||
StatusApproved CitizenshipApplicationStatus = "approved"
|
||||
StatusRejected CitizenshipApplicationStatus = "rejected"
|
||||
)
|
||||
|
||||
type CitizenshipApplication struct {
|
||||
Id uuid.UUID `db:"id" json:"id"`
|
||||
UserId uuid.UUID `db:"user_id" json:"user_id"`
|
||||
Status CitizenshipApplicationStatus `db:"status" json:"status"`
|
||||
RegionId uuid.UUID `db:"region_id" json:"region_id"`
|
||||
ReviewedBy *uuid.UUID `db:"reviewed_by" json:"reviewed_by,omitempty"`
|
||||
ReviewedAt *time.Time `db:"reviewed_at" json:"reviewed_at,omitempty"`
|
||||
RejectReason *string `db:"reject_reason" json:"reject_reason,omitempty"`
|
||||
CreatedAt time.Time `db:"created_at" json:"created_at"`
|
||||
}
|
||||
|
||||
type CreateCitizenshipApplicationInput struct {
|
||||
RegionID uuid.UUID `json:"region_id" binding:"required"`
|
||||
}
|
||||
|
||||
type ReviewCitizenshipApplicationInput struct {
|
||||
Status CitizenshipApplicationStatus `json:"status" binding:"required"`
|
||||
RejectReason *string `json:"reject_reason"`
|
||||
}
|
||||
@@ -1,119 +0,0 @@
|
||||
package models
|
||||
|
||||
type FieldType string
|
||||
|
||||
const (
|
||||
FieldTypeText FieldType = "text"
|
||||
FieldTypeDate FieldType = "date"
|
||||
FieldTypeSelect FieldType = "select"
|
||||
FieldTypeMasked FieldType = "masked"
|
||||
)
|
||||
|
||||
type LocalizedString struct {
|
||||
En string `json:"en"`
|
||||
Ru string `json:"ru"`
|
||||
It string `json:"it"`
|
||||
}
|
||||
|
||||
type FieldOption struct {
|
||||
Value string `json:"value"`
|
||||
Label LocalizedString `json:"label"`
|
||||
}
|
||||
|
||||
type FieldTemplate struct {
|
||||
ID string `json:"id"`
|
||||
Name LocalizedString `json:"name"`
|
||||
Type FieldType `json:"type"`
|
||||
Required bool `json:"required"`
|
||||
Mask *string `json:"mask,omitempty"`
|
||||
Options []FieldOption `json:"options,omitempty"`
|
||||
Generated bool `json:"generated"`
|
||||
}
|
||||
|
||||
type DocumentType struct {
|
||||
ID uuid.UUID `db:"id" json:"id"`
|
||||
Code string `db:"code" json:"code"`
|
||||
EmblemURL string `db:"emblem_url" json:"emblem_url"`
|
||||
Color string `db:"color" json:"color"`
|
||||
Fields []FieldTemplate `db:"fields" json:"fields"`
|
||||
CreatedAt time.Time `db:"created_at" json:"created_at"`
|
||||
}
|
||||
|
||||
type DocumentApplicationStatus string
|
||||
|
||||
const (
|
||||
ApplicationStatusPending DocumentApplicationStatus = "pending"
|
||||
ApplicationStatusApproved DocumentApplicationStatus = "approved"
|
||||
ApplicationStatusRejected DocumentApplicationStatus = "rejected"
|
||||
)
|
||||
|
||||
type DocumentApplication struct {
|
||||
ID uuid.UUID `db:"id" json:"id"`
|
||||
UserID uuid.UUID `db:"user_id" json:"user_id"`
|
||||
DocumentTypeID uuid.UUID `db:"document_type_id" json:"document_type_id"`
|
||||
Status DocumentApplicationStatus `db:"status" json:"status"`
|
||||
ReviewedBy *uuid.UUID `db:"reviewed_by" json:"reviewed_by,omitempty"`
|
||||
ReviewedAt *time.Time `db:"reviewed_at" json:"reviewed_at,omitempty"`
|
||||
RejectReason *string `db:"reject_reason" json:"reject_reason,omitempty"`
|
||||
Data map[string]string `db:"data" json:"data"`
|
||||
|
||||
CreatedAt time.Time `db:"created_at" json:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
|
||||
}
|
||||
|
||||
type DocumentApplicationLocalized struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
UserID uuid.UUID `json:"user_id"`
|
||||
DocumentType DocumentType `json:"document_type"`
|
||||
Status DocumentApplicationStatus `json:"status"`
|
||||
ReviewedBy *uuid.UUID `json:"reviewed_by,omitempty"`
|
||||
ReviewedAt *time.Time `json:"reviewed_at,omitempty"`
|
||||
RejectReason *string `json:"reject_reason,omitempty"`
|
||||
Data map[string]string `json:"data"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type Document struct {
|
||||
ID uuid.UUID `db:"id" json:"id"`
|
||||
UserID uuid.UUID `db:"user_id" json:"user_id"`
|
||||
DocumentTypeID uuid.UUID `db:"document_type_id" json:"document_type_id"`
|
||||
ApplicationID uuid.UUID `db:"application_id" json:"application_id"`
|
||||
Number string `db:"number" json:"number"`
|
||||
PhotoURL *string `db:"photo_url" json:"photo_url,omitempty"`
|
||||
Data map[string]string `db:"data" json:"data"`
|
||||
IssuedAt time.Time `db:"issued_at" json:"issued_at"`
|
||||
RevokedAt *time.Time `db:"revoked_at" json:"revoked_at,omitempty"`
|
||||
RevokedBy *uuid.UUID `db:"revoked_by" json:"revoked_by,omitempty"`
|
||||
CreatedAt time.Time `db:"created_at" json:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at" json:"updated_at"`
|
||||
}
|
||||
|
||||
type DocumentLocalized struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
UserID uuid.UUID `json:"user_id"`
|
||||
DocumentType DocumentType `json:"document_type"`
|
||||
ApplicationID uuid.UUID `json:"application_id"`
|
||||
Number string `json:"number"`
|
||||
PhotoURL *string `json:"photo_url,omitempty"`
|
||||
Data map[string]string `json:"data"`
|
||||
IssuedAt time.Time `json:"issued_at"`
|
||||
RevokedAt *time.Time `json:"revoked_at,omitempty"`
|
||||
RevokedBy *uuid.UUID `json:"revoked_by,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type CreateDocumentApplicationInput struct {
|
||||
DocumentTypeCode string `json:"document_type_code" binding:"required"`
|
||||
Data map[string]string `json:"data" binding:"required"`
|
||||
}
|
||||
|
||||
type ReviewDocumentApplicationInput struct {
|
||||
Status DocumentApplicationStatus `json:"status" binding:"required"`
|
||||
RejectReason *string `json:"reject_reason" binding:"omitempty"`
|
||||
}
|
||||
|
||||
type RevokeDocumentInput struct {
|
||||
DocumentID uuid.UUID `json:"document_id" binding:"required"`
|
||||
}
|
||||
@@ -1,5 +1,11 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Region struct {
|
||||
ID uuid.UUID `db:"id" json:"id"`
|
||||
Code string `db:"code" json:"code"`
|
||||
@@ -16,4 +22,4 @@ type RegionLocalized struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/dusiburg/kotosoft-id/internal/models"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type CitizenshipRepository struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewCitizenshipRepository(db *pgxpool.Pool) *CitizenshipRepository {
|
||||
return &CitizenshipRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *CitizenshipRepository) GetApplicationByUserId(ctx context.Context, userID uuid.UUID) (*models.CitizenshipApplication, error) {
|
||||
var a models.CitizenshipApplication
|
||||
|
||||
err := r.db.QueryRow(
|
||||
ctx,
|
||||
`SELECT id, user_id, status, region_id, reviewed_by, reviewed_at, reject_reason, created_at
|
||||
FROM citizenship_applications WHERE user_id = $1`,
|
||||
userID,
|
||||
).Scan(
|
||||
&a.Id, &a.UserId, &a.Status, &a.RegionId,
|
||||
&a.ReviewedBy, &a.ReviewedAt, &a.RejectReason, &a.CreatedAt,
|
||||
)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &a, nil
|
||||
}
|
||||
|
||||
func (r *CitizenshipRepository) GetApplicationById(ctx context.Context, id uuid.UUID) (*models.CitizenshipApplication, error) {
|
||||
var a models.CitizenshipApplication
|
||||
|
||||
err := r.db.QueryRow(
|
||||
ctx,
|
||||
`SELECT id, user_id, status, region_id, reviewed_by, reviewed_at, reject_reason, created_at
|
||||
FROM citizenship_applications WHERE id = $1`,
|
||||
id,
|
||||
).Scan(
|
||||
&a.Id, &a.UserId, &a.Status, &a.RegionId,
|
||||
&a.ReviewedBy, &a.ReviewedAt, &a.RejectReason, &a.CreatedAt,
|
||||
)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &a, nil
|
||||
}
|
||||
|
||||
func (r *CitizenshipRepository) CreateApplication(ctx context.Context, application models.CitizenshipApplication) (*models.CitizenshipApplication, error) {
|
||||
err := r.db.QueryRow(
|
||||
ctx,
|
||||
`INSERT INTO citizenship_applications (user_id, region_id, status)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id, status, created_at`,
|
||||
application.UserId, application.RegionId, application.Status,
|
||||
).Scan(&application.Id, &application.Status, &application.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &application, nil
|
||||
}
|
||||
|
||||
func (r *CitizenshipRepository) UpdateApplication(ctx context.Context, id uuid.UUID, status models.CitizenshipApplicationStatus, reviewedBy uuid.UUID, rejectReason *string) error {
|
||||
_, err := r.db.Exec(ctx, `
|
||||
UPDATE citizenship_applications
|
||||
SET status = $1, reviewed_by = $2, reviewed_at = now(), reject_reason = $3
|
||||
WHERE id = $4
|
||||
`, status, reviewedBy, rejectReason, id)
|
||||
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/dusiburg/kotosoft-id/internal/models"
|
||||
"github.com/dusiburg/kotosoft-id/internal/repositories"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type CitizenshipService struct {
|
||||
r *repositories.CitizenshipRepository
|
||||
}
|
||||
|
||||
func NewCitizenshipService(r *repositories.CitizenshipRepository) *CitizenshipService {
|
||||
return &CitizenshipService{r: r}
|
||||
}
|
||||
|
||||
func (s *CitizenshipService) GetCitizenshipApplicationByUserId(ctx context.Context, userID uuid.UUID) (*models.CitizenshipApplication, error) {
|
||||
application, err := s.r.GetApplicationByUserId(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
if application == nil {
|
||||
return nil, models.ErrNotFound
|
||||
}
|
||||
|
||||
return application, nil
|
||||
}
|
||||
|
||||
func (s *CitizenshipService) CreateCitizenshipApplication(ctx context.Context, userID, regionID uuid.UUID) (*models.CitizenshipApplication, error) {
|
||||
exists, err := s.r.GetApplicationByUserId(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
if exists != nil {
|
||||
return nil, models.ErrAlreadyExists
|
||||
}
|
||||
|
||||
application, err := s.r.CreateApplication(ctx, models.CitizenshipApplication{
|
||||
UserId: userID,
|
||||
Status: models.StatusPending,
|
||||
RegionId: regionID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
|
||||
return application, nil
|
||||
}
|
||||
|
||||
func (s *CitizenshipService) ReviewCitizenshipApplication(ctx context.Context, applicationID, reviewerID uuid.UUID, input models.ReviewCitizenshipApplicationInput) (*models.CitizenshipApplication, error) {
|
||||
if input.Status != models.StatusApproved && input.Status != models.StatusRejected {
|
||||
return nil, models.ErrBadRequest
|
||||
}
|
||||
if input.Status == models.StatusRejected && (input.RejectReason == nil || *input.RejectReason == "") {
|
||||
return nil, models.ErrBadRequest.WithMessage("Reject reason is required")
|
||||
}
|
||||
|
||||
application, err := s.r.GetApplicationById(ctx, applicationID)
|
||||
if err != nil {
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
if application == nil {
|
||||
return nil, models.ErrNotFound
|
||||
}
|
||||
if application.Status != models.StatusPending {
|
||||
return nil, models.ErrBadRequest.WithMessage("Application is not pending")
|
||||
}
|
||||
|
||||
rejectReason := input.RejectReason
|
||||
if input.Status == models.StatusApproved {
|
||||
rejectReason = nil
|
||||
}
|
||||
|
||||
if err := s.r.UpdateApplication(ctx, applicationID, input.Status, reviewerID, rejectReason); err != nil {
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
|
||||
updated, err := s.r.GetApplicationById(ctx, applicationID)
|
||||
if err != nil {
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
|
||||
return updated, nil
|
||||
}
|
||||
@@ -327,7 +327,6 @@ func (s *Service) issuePair(ctx context.Context, user *models.User, clientID uui
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
|
||||
// Track issued access tokens per user and client to support revocation.
|
||||
_ = s.redis.SAdd(ctx, fmt.Sprintf("user_client_tokens:%s:%s", user.ID, clientID), accessToken).Err()
|
||||
|
||||
_, err = s.oauth.CreateRefreshToken(ctx, models.RefreshTokenRow{
|
||||
|
||||
@@ -140,6 +140,26 @@ CREATE TABLE IF NOT EXISTS public.permission_translations (
|
||||
PRIMARY KEY (permission_id, locale)
|
||||
);
|
||||
|
||||
CREATE TABLE citizenship_applications (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id uuid NOT NULL UNIQUE REFERENCES users(id) ON DELETE CASCADE,
|
||||
status text NOT NULL DEFAULT 'pending',
|
||||
region_id uuid REFERENCES regions(id),
|
||||
reviewed_by uuid REFERENCES users(id),
|
||||
reviewed_at timestamptz,
|
||||
reject_reason text,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
);
|
||||
|
||||
CREATE TABLE citizens (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id uuid NOT NULL UNIQUE REFERENCES users(id),
|
||||
application_id uuid NOT NULL REFERENCES citizenship_applications(id),
|
||||
number text NOT NULL UNIQUE,
|
||||
issued_at timestamptz NOT NULL DEFAULT now(),
|
||||
region_id uuid REFERENCES regions(id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_permission_translations_permission_id
|
||||
ON public.permission_translations (permission_id);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user