ch users <-> branches
This commit is contained in:
1 parent
d3e188affe
commit
0971158e98
9 files changed
+43
-18
No files matched your search
@@ -23,24 +23,16 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) userbranch.
|
||||
}
|
||||
|
||||
func (r *repository) Create(ctx context.Context, dto userbranch.CreateUserBranchDTO) (userbranch.UserBranchDTO, error) {
|
||||
// users <-> branches is many-to-many: a user keeps every other branch
|
||||
// assignment they already have, this only adds (or, if already assigned,
|
||||
// idempotently touches) the one pair given here.
|
||||
query := `
|
||||
WITH existing AS (
|
||||
SELECT id FROM branch_user WHERE user_id = $2 AND deleted_at IS NULL ORDER BY id LIMIT 1
|
||||
), updated AS (
|
||||
UPDATE branch_user SET
|
||||
branch_id = $1,
|
||||
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
||||
WHERE id = (SELECT id FROM existing)
|
||||
RETURNING id, branch_id, user_id, created_at, updated_at
|
||||
), inserted AS (
|
||||
WITH upserted AS (
|
||||
INSERT INTO branch_user (branch_id, user_id)
|
||||
SELECT $1, $2
|
||||
WHERE NOT EXISTS (SELECT 1 FROM existing)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT (branch_id, user_id) WHERE deleted_at IS NULL
|
||||
DO UPDATE SET updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
||||
RETURNING id, branch_id, user_id, created_at, updated_at
|
||||
), upserted AS (
|
||||
SELECT * FROM updated
|
||||
UNION ALL
|
||||
SELECT * FROM inserted
|
||||
)
|
||||
SELECT upserted.id, upserted.branch_id, b.name, upserted.user_id, u.name, upserted.created_at, upserted.updated_at
|
||||
FROM upserted
|
||||
|
||||
@@ -33,7 +33,7 @@ func (h *handler) Register(router *gin.RouterGroup) {
|
||||
|
||||
// create godoc
|
||||
// @Summary Assign user to branch
|
||||
// @Description Creates a branch-user assignment, or updates the user's existing branch if one is already assigned
|
||||
// @Description Assigns a user to a branch. Users and branches are many-to-many: a user keeps their other branch assignments, and re-assigning the same pair is a no-op.
|
||||
// @Tags branch-users
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
|
||||
Reference in new issue
Block a user