Files
kotosoft-id/kotosoft-id-backend/internal/repositories/positions.go
T
2026-03-09 18:09:02 +03:00

129 lines
3.0 KiB
Go

package repositories
import (
"context"
"strings"
"github.com/dusiburg/kotosoft-id/internal/models"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgxpool"
)
type PositionRepository struct {
db *pgxpool.Pool
}
func NewPositionRepository(db *pgxpool.Pool) *PositionRepository {
return &PositionRepository{db: db}
}
func (r *PositionRepository) GetUserPositions(ctx context.Context, userID uuid.UUID, locale string) ([]models.PositionLocalized, error) {
rows, err := r.db.Query(ctx, `
SELECT p.code,
COALESCE(pt.name, p.code) AS name, pt.description
FROM user_positions up
JOIN positions p ON up.position_id = p.id
LEFT JOIN position_translations pt
ON pt.position_id = p.id
AND pt.locale = $2
WHERE up.user_id = $1
AND (up.valid_to IS NULL OR up.valid_to > now())
ORDER BY name
`, userID, strings.ToLower(locale))
if err != nil {
return nil, err
}
defer rows.Close()
var result []models.PositionLocalized
for rows.Next() {
var p models.PositionLocalized
if err := rows.Scan(&p.Code, &p.Name, &p.Description); err != nil {
return nil, err
}
result = append(result, p)
}
return result, rows.Err()
}
func (r *PositionRepository) GetAllPositions(ctx context.Context, locale string) ([]models.PositionLocalized, error) {
rows, err := r.db.Query(ctx, `
SELECT p.code,
COALESCE(pt.name, p.code) AS name, pt.description
FROM positions p
LEFT JOIN position_translations pt
ON pt.position_id = p.id
AND pt.locale = $1
ORDER BY name
`, strings.ToLower(locale))
if err != nil {
return nil, err
}
defer rows.Close()
var result []models.PositionLocalized
for rows.Next() {
var p models.PositionLocalized
if err := rows.Scan(&p.Code, &p.Name, &p.Description); err != nil {
return nil, err
}
result = append(result, p)
}
return result, rows.Err()
}
func (r *PositionRepository) UpsertPositionForLocale(ctx context.Context, code, locale, name string, description *string) error {
tx, err := r.db.Begin(ctx)
if err != nil {
return err
}
defer tx.Rollback(ctx)
var positionID uuid.UUID
err = tx.QueryRow(ctx, `
INSERT INTO positions (code)
VALUES ($1)
ON CONFLICT (code) DO UPDATE
SET updated_at = now()
RETURNING id
`, code).Scan(&positionID)
if err != nil {
return err
}
_, err = tx.Exec(ctx, `
INSERT INTO position_translations (position_id, locale, name, description)
VALUES ($1, $2, $3, $4)
ON CONFLICT (position_id, locale) DO UPDATE
SET name = EXCLUDED.name,
description = EXCLUDED.description
`, positionID, strings.ToLower(locale), name, description)
if err != nil {
return err
}
return tx.Commit(ctx)
}
func (r *PositionRepository) UserHasPosition(ctx context.Context, userID uuid.UUID, code string) (bool, error) {
var exists bool
err := r.db.QueryRow(ctx, `
SELECT EXISTS (
SELECT 1
FROM user_positions up
JOIN positions p ON up.position_id = p.id
WHERE up.user_id = $1
AND p.code = $2
AND (up.valid_to IS NULL OR up.valid_to > now())
)
`, userID, code).Scan(&exists)
if err != nil {
return false, err
}
return exists, nil
}