Fix partial team member updates clearing team_role on invite accept.

Use nullable sqlc.narg fields so empty strings are not written to team_role and other optional columns.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Yared Yemane 2026-05-22 07:54:44 -07:00
parent 215a4bd1dc
commit 176f78515d
3 changed files with 39 additions and 32 deletions

View File

@ -98,22 +98,22 @@ WHERE (
-- name: UpdateTeamMember :exec
UPDATE team_members
SET
first_name = COALESCE($1, first_name),
last_name = COALESCE($2, last_name),
phone_number = COALESCE($3, phone_number),
team_role = COALESCE($4, team_role),
department = COALESCE($5, department),
job_title = COALESCE($6, job_title),
employment_type = COALESCE($7, employment_type),
hire_date = COALESCE($8, hire_date),
profile_picture_url = COALESCE($9, profile_picture_url),
bio = COALESCE($10, bio),
work_phone = COALESCE($11, work_phone),
emergency_contact = COALESCE($12, emergency_contact),
permissions = COALESCE($13, permissions),
updated_by = $14,
first_name = COALESCE(sqlc.narg('first_name')::VARCHAR, first_name),
last_name = COALESCE(sqlc.narg('last_name')::VARCHAR, last_name),
phone_number = COALESCE(sqlc.narg('phone_number'), phone_number),
team_role = COALESCE(sqlc.narg('team_role')::VARCHAR, team_role),
department = COALESCE(sqlc.narg('department'), department),
job_title = COALESCE(sqlc.narg('job_title'), job_title),
employment_type = COALESCE(sqlc.narg('employment_type'), employment_type),
hire_date = COALESCE(sqlc.narg('hire_date'), hire_date),
profile_picture_url = COALESCE(sqlc.narg('profile_picture_url'), profile_picture_url),
bio = COALESCE(sqlc.narg('bio'), bio),
work_phone = COALESCE(sqlc.narg('work_phone'), work_phone),
emergency_contact = COALESCE(sqlc.narg('emergency_contact'), emergency_contact),
permissions = COALESCE(sqlc.narg('permissions'), permissions),
updated_by = @updated_by,
updated_at = CURRENT_TIMESTAMP
WHERE id = $15;
WHERE id = @id;
-- name: UpdateTeamMemberStatus :exec
UPDATE team_members

View File

@ -637,10 +637,10 @@ func (q *Queries) SearchTeamMembers(ctx context.Context, arg SearchTeamMembersPa
const UpdateTeamMember = `-- name: UpdateTeamMember :exec
UPDATE team_members
SET
first_name = COALESCE($1, first_name),
last_name = COALESCE($2, last_name),
first_name = COALESCE($1::VARCHAR, first_name),
last_name = COALESCE($2::VARCHAR, last_name),
phone_number = COALESCE($3, phone_number),
team_role = COALESCE($4, team_role),
team_role = COALESCE($4::VARCHAR, team_role),
department = COALESCE($5, department),
job_title = COALESCE($6, job_title),
employment_type = COALESCE($7, employment_type),
@ -656,10 +656,10 @@ WHERE id = $15
`
type UpdateTeamMemberParams struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
FirstName pgtype.Text `json:"first_name"`
LastName pgtype.Text `json:"last_name"`
PhoneNumber pgtype.Text `json:"phone_number"`
TeamRole string `json:"team_role"`
TeamRole pgtype.Text `json:"team_role"`
Department pgtype.Text `json:"department"`
JobTitle pgtype.Text `json:"job_title"`
EmploymentType pgtype.Text `json:"employment_type"`

View File

@ -178,18 +178,18 @@ func (s *Store) UpdateTeamMember(ctx context.Context, req domain.UpdateTeamMembe
}
return s.queries.UpdateTeamMember(ctx, dbgen.UpdateTeamMemberParams{
FirstName: req.FirstName,
LastName: req.LastName,
PhoneNumber: pgtype.Text{String: req.PhoneNumber, Valid: req.PhoneNumber != ""},
TeamRole: req.TeamRole,
Department: pgtype.Text{String: req.Department, Valid: req.Department != ""},
JobTitle: pgtype.Text{String: req.JobTitle, Valid: req.JobTitle != ""},
EmploymentType: pgtype.Text{String: req.EmploymentType, Valid: req.EmploymentType != ""},
FirstName: optionalPgText(req.FirstName),
LastName: optionalPgText(req.LastName),
PhoneNumber: optionalPgText(req.PhoneNumber),
TeamRole: optionalPgText(req.TeamRole),
Department: optionalPgText(req.Department),
JobTitle: optionalPgText(req.JobTitle),
EmploymentType: optionalPgText(req.EmploymentType),
HireDate: hireDate,
ProfilePictureUrl: pgtype.Text{String: req.ProfilePictureURL, Valid: req.ProfilePictureURL != ""},
Bio: pgtype.Text{String: req.Bio, Valid: req.Bio != ""},
WorkPhone: pgtype.Text{String: req.WorkPhone, Valid: req.WorkPhone != ""},
EmergencyContact: pgtype.Text{String: req.EmergencyContact, Valid: req.EmergencyContact != ""},
ProfilePictureUrl: optionalPgText(req.ProfilePictureURL),
Bio: optionalPgText(req.Bio),
WorkPhone: optionalPgText(req.WorkPhone),
EmergencyContact: optionalPgText(req.EmergencyContact),
Permissions: permissionsJSON,
UpdatedBy: pgtype.Int8{Int64: req.UpdatedBy, Valid: req.UpdatedBy > 0},
ID: req.TeamMemberID,
@ -516,3 +516,10 @@ func (s *Store) GetTeamRefreshTokenByToken(ctx context.Context, token string) (d
func (s *Store) RevokeTeamRefreshTokenByToken(ctx context.Context, token string) error {
return s.queries.RevokeTeamRefreshTokenByToken(ctx, token)
}
func optionalPgText(value string) pgtype.Text {
if value == "" {
return pgtype.Text{}
}
return pgtype.Text{String: value, Valid: true}
}