🐛 Backfill missing employee suppliers before automatic project enrollment
This commit is contained in:
parent
2b7e081c39
commit
7f688a3bc1
@ -16,15 +16,15 @@
|
||||
|
||||
- 账号与项目成员均为空:建议补为默认供应商。
|
||||
- 账号有有效供应商、成员为空:仅建议补成员,不改角色、不新增成员。
|
||||
- 非空归属冲突、失效供应商、账号为空但成员已有供应商:列出并保留现值,不猜测。
|
||||
- 系统管理账号不进入员工补齐清单;停用账号单独显示状态,是否补齐需确认,不重新启用。
|
||||
- 启动时确保默认供应商存在,并补齐其在职生产员工缺失的项目成员记录;不自动填补或更换员工账号供应商。旧工作组迁移不再推断或改写员工供应商。
|
||||
- 员工账号为空时一律补为默认供应商,即使已有项目成员是其他供应商;已有非空项目归属保留并在预览中列出冲突,不擅自转移。失效、跨组织的非空供应商不替换,也不复制到空成员。
|
||||
- 系统管理账号不进入员工补齐清单;停用员工只补空归属,不重新启用、不新增项目权限。
|
||||
- 启动时先补齐员工与项目成员的空归属,再为默认供应商在职生产员工补缺失的项目成员。字段级条件更新仅匹配缺失、null、空字符串或纯空白 supplierId,可重复运行;不改变已有角色、密码、状态或任务归属。旧工作组迁移不再推断或改写员工供应商。
|
||||
|
||||
## 验证与发布
|
||||
|
||||
- 验证创建必选、管理员不可变更、成员继承、删除后会话失效且历史保留、默认与普通供应商领取及质检路由。
|
||||
- 单元测试、内存 API 与本地页面验证不等于 Fanyu 真实完整流程验收。
|
||||
- 先确认历史清单、备份对应记录,再执行字段级迁移和部署;本次代码实现不直接迁移线上数据。
|
||||
- 发布前预览并备份对应归属字段;API/Worker 启动执行上述已确认的补空规则。创建 API 继续拒绝空供应商,前端默认选中真实的默认供应商;不能用显示名称代替持久化归属。
|
||||
- Fanyu 保留 `k8s/fanyu-api-recreate.patch.yaml` 的先停旧后启新策略。完整 manifest 应用前保留 `Recreate` 并删除 `rollingUpdate`,不被通用模板覆盖。
|
||||
- 发布有短暂不可用窗口;验证新实例、镜像、健康及业务后再恢复操作。回退只用已验证且遵守固定归属规则的镜像,不回退到允许更换供应商的版本。
|
||||
- 不改视频处理、切片交互、算子和工作流;此前暂不处理的领取同步失败恢复问题不在本次范围。
|
||||
|
||||
@ -189,3 +189,30 @@ func TestNewProjectAutomaticallyIncludesDefaultSupplierStaff(t *testing.T) {
|
||||
t.Fatal("external supplier staff joined without authorization")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmployeeSupplierCreateRejectsEveryBlankRepresentation(t *testing.T) {
|
||||
t.Setenv("DROBOTICFLOW_REQUIRE_SESSION_AUTH", "true")
|
||||
ds := store.NewMemoryStore()
|
||||
handler := NewHandler(ds, nil)
|
||||
root := bootstrapSuperAdminSession(t, handler)
|
||||
org := adminCreateOrganization(t, handler, root, "Blank supplier", "blank-supplier")
|
||||
admin := adminCreateUser(t, handler, root, org.ID, "Admin", "blank-admin@example.com", domain.UserRoleAdmin, "test-password")
|
||||
adminSession := loginSession(t, handler, admin.Email, "test-password")
|
||||
before, _ := ds.ListUsers(t.Context())
|
||||
for _, value := range []any{nil, "", " \t\n"} {
|
||||
for _, caller := range []struct {
|
||||
path string
|
||||
cookie *http.Cookie
|
||||
}{{"/api/users", adminSession}, {"/api/admin/organizations/" + org.ID + "/users", root}} {
|
||||
body, _ := json.Marshal(map[string]any{"name": "Blank", "email": "blank-staff@example.com", "role": "annotator", "password": "test-password", "supplierId": value})
|
||||
res := serveJSONWithCookie(handler, http.MethodPost, caller.path, body, caller.cookie)
|
||||
if res.Code != http.StatusBadRequest {
|
||||
t.Fatalf("saved blank supplier: %d %s", res.Code, res.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
after, _ := ds.ListUsers(t.Context())
|
||||
if len(after) != len(before) {
|
||||
t.Fatal("rejected request created an employee")
|
||||
}
|
||||
}
|
||||
|
||||
106
internal/store/employee_supplier_backfill.go
Normal file
106
internal/store/employee_supplier_backfill.go
Normal file
@ -0,0 +1,106 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"droboticflow/internal/domain"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
type employeeSupplierBackfill struct {
|
||||
Collection string
|
||||
ID string
|
||||
UserID string
|
||||
SupplierID string
|
||||
}
|
||||
|
||||
func isSupplierEmployee(user domain.UserRecord) bool {
|
||||
if user.IsSuperAdmin {
|
||||
return false
|
||||
}
|
||||
switch user.Role {
|
||||
case domain.UserRoleAnnotator, domain.UserRoleQualityReviewer, domain.UserRoleAcceptanceReviewer, domain.UserRoleSupplierManager, "member":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Fill blanks only. Historical nonempty conflicts are retained, not interpreted
|
||||
// as permission to transfer the employee or rewrite an existing membership.
|
||||
func planEmployeeSupplierBackfill(org string, users []domain.UserRecord, members []domain.ProjectMemberRecord, suppliers []domain.SupplierRecord) []employeeSupplierBackfill {
|
||||
valid := map[string]bool{domain.DefaultSupplierID(normalizedOrganizationID(org)): true}
|
||||
for _, supplier := range suppliers {
|
||||
if annotationOrganizationMatches(supplier.OrganizationID, org) && supplier.Status == "active" {
|
||||
valid[supplier.ID] = true
|
||||
}
|
||||
}
|
||||
var changes []employeeSupplierBackfill
|
||||
for _, user := range users {
|
||||
if !annotationOrganizationMatches(user.OrganizationID, org) || !isSupplierEmployee(user) {
|
||||
continue
|
||||
}
|
||||
target := strings.TrimSpace(user.SupplierID)
|
||||
if target == "" {
|
||||
target = domain.DefaultSupplierID(normalizedOrganizationID(org))
|
||||
changes = append(changes, employeeSupplierBackfill{Collection: "users", ID: user.ID, SupplierID: target})
|
||||
}
|
||||
if !valid[target] {
|
||||
continue // Do not spread a dangling or foreign supplier reference.
|
||||
}
|
||||
for _, member := range members {
|
||||
if member.UserID == user.ID && annotationOrganizationMatches(member.OrganizationID, org) && strings.TrimSpace(member.SupplierID) == "" {
|
||||
changes = append(changes, employeeSupplierBackfill{Collection: "projectMembers", ID: member.ID, UserID: user.ID, SupplierID: target})
|
||||
}
|
||||
}
|
||||
}
|
||||
return changes
|
||||
}
|
||||
|
||||
func employeeSupplierBackfillFilter(org string, change employeeSupplierBackfill) bson.M {
|
||||
identity := bson.M{"id": change.ID}
|
||||
if change.Collection == "users" {
|
||||
identity["isSuperAdmin"] = bson.M{"$ne": true}
|
||||
identity["role"] = bson.M{"$in": bson.A{"annotator", "quality_reviewer", "acceptance_reviewer", "supplier_manager", "member"}}
|
||||
} else {
|
||||
identity["userId"] = change.UserID
|
||||
}
|
||||
return bson.M{"$and": bson.A{
|
||||
orgCompatibilityFilter(org), identity,
|
||||
bson.M{"$or": bson.A{bson.M{"supplierId": nil}, bson.M{"supplierId": bson.M{"$regex": `^\s*$`}}}},
|
||||
}}
|
||||
}
|
||||
|
||||
func (store *MongoStore) backfillMissingEmployeeSuppliers(ctx context.Context, org string) error {
|
||||
if _, err := store.EnsureDefaultSupplier(ctx, org); err != nil {
|
||||
return err
|
||||
}
|
||||
users, err := store.ListUsersByOrganization(ctx, org)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
members, err := store.ListProjectMembers(ctx, org, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
suppliers, err := store.ListSuppliers(ctx, org)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return store.applyEmployeeSupplierBackfill(ctx, org, planEmployeeSupplierBackfill(org, users, members, suppliers))
|
||||
}
|
||||
|
||||
func (store *MongoStore) applyEmployeeSupplierBackfill(ctx context.Context, org string, changes []employeeSupplierBackfill) error {
|
||||
for _, change := range changes {
|
||||
collection := store.users
|
||||
if change.Collection == "projectMembers" {
|
||||
collection = store.projectMembers
|
||||
}
|
||||
// Compare-and-set without upsert: never resurrect a deleted record or
|
||||
// overwrite a supplier set by another process. Retries are idempotent.
|
||||
if _, err := collection.UpdateOne(ctx, employeeSupplierBackfillFilter(org, change), bson.M{"$set": bson.M{"supplierId": change.SupplierID, "updatedAt": nowISO()}}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
109
internal/store/employee_supplier_backfill_test.go
Normal file
109
internal/store/employee_supplier_backfill_test.go
Normal file
@ -0,0 +1,109 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"droboticflow/internal/domain"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
|
||||
)
|
||||
|
||||
func TestEmployeeSupplierBackfillOnlyFillsBlanks(t *testing.T) {
|
||||
org := "org-backfill"
|
||||
def := domain.DefaultSupplierID(org)
|
||||
users := []domain.UserRecord{
|
||||
{ID: "empty", OrganizationID: org, Role: domain.UserRoleAnnotator, Status: domain.UserStatusActive, PasswordHash: "unchanged-hash"},
|
||||
{ID: "off", OrganizationID: org, Role: domain.UserRoleQualityReviewer, Status: domain.UserStatusDisabled, SupplierID: " \t"},
|
||||
{ID: "external", OrganizationID: org, Role: domain.UserRoleAnnotator, SupplierID: "vendor"},
|
||||
{ID: "dangling", OrganizationID: org, Role: domain.UserRoleAnnotator, SupplierID: "missing-vendor"},
|
||||
{ID: "foreign-owner", OrganizationID: org, Role: domain.UserRoleAnnotator, SupplierID: "foreign-vendor"},
|
||||
{ID: "admin", OrganizationID: org, Role: domain.UserRoleAdmin},
|
||||
{ID: "super", OrganizationID: org, Role: domain.UserRoleAnnotator, IsSuperAdmin: true},
|
||||
{ID: "outside", OrganizationID: "other", Role: domain.UserRoleAnnotator},
|
||||
}
|
||||
members := []domain.ProjectMemberRecord{
|
||||
{ID: "blank", OrganizationID: org, ProjectID: "p", UserID: "empty", Roles: []domain.AnnotationAssignmentRole{domain.AnnotationRoleQualityReviewer}},
|
||||
{ID: "conflict", OrganizationID: org, ProjectID: "p2", UserID: "empty", SupplierID: "vendor"},
|
||||
{ID: "owned-blank", OrganizationID: org, UserID: "external", SupplierID: " \n"},
|
||||
{ID: "cross-org", OrganizationID: "other", UserID: "empty"},
|
||||
{ID: "dangling-member", OrganizationID: org, UserID: "dangling"},
|
||||
{ID: "foreign-member", OrganizationID: org, UserID: "foreign-owner"},
|
||||
}
|
||||
suppliers := []domain.SupplierRecord{{ID: "vendor", OrganizationID: org, Status: "active"}, {ID: "foreign-vendor", OrganizationID: "other", Status: "active"}}
|
||||
beforeUsers, beforeMembers := clone(users), clone(members)
|
||||
changes := planEmployeeSupplierBackfill(org, users, members, suppliers)
|
||||
want := []employeeSupplierBackfill{
|
||||
{Collection: "users", ID: "empty", SupplierID: def},
|
||||
{Collection: "projectMembers", ID: "blank", UserID: "empty", SupplierID: def},
|
||||
{Collection: "users", ID: "off", SupplierID: def},
|
||||
{Collection: "projectMembers", ID: "owned-blank", UserID: "external", SupplierID: "vendor"},
|
||||
}
|
||||
if !reflect.DeepEqual(changes, want) || !reflect.DeepEqual(users, beforeUsers) || !reflect.DeepEqual(members, beforeMembers) {
|
||||
t.Fatalf("wrong plan or mutated inventory: %#v", changes)
|
||||
}
|
||||
// Apply only the two allowed fields, then prove repeat planning is empty.
|
||||
for _, c := range changes {
|
||||
for i := range users {
|
||||
if c.Collection == "users" && users[i].ID == c.ID {
|
||||
users[i].SupplierID = c.SupplierID
|
||||
}
|
||||
}
|
||||
for i := range members {
|
||||
if c.Collection == "projectMembers" && members[i].ID == c.ID {
|
||||
members[i].SupplierID = c.SupplierID
|
||||
}
|
||||
}
|
||||
}
|
||||
if again := planEmployeeSupplierBackfill(org, users, members, suppliers); len(again) != 0 {
|
||||
t.Fatalf("non-idempotent: %#v", again)
|
||||
}
|
||||
if users[1].Status != domain.UserStatusDisabled || users[0].PasswordHash != beforeUsers[0].PasswordHash || !reflect.DeepEqual(members[0].Roles, beforeMembers[0].Roles) {
|
||||
t.Fatal("backfill changed employee lifecycle or roles")
|
||||
}
|
||||
if len(defaultSupplierMemberRoles(users[1])) != 0 || len(defaultSupplierMemberRoles(users[0])) == 0 {
|
||||
t.Fatal("only active default staff should auto-enroll")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmployeeSupplierBackfillMongoWritesAreConditionalAndFieldOnly(t *testing.T) {
|
||||
mt := mtest.New(t, mtest.NewOptions().ClientType(mtest.Mock))
|
||||
mt.Run("blank-only no upsert", func(mt *mtest.T) {
|
||||
ds := &MongoStore{users: mt.DB.Collection("users"), projectMembers: mt.DB.Collection("projectMembers")}
|
||||
changes := []employeeSupplierBackfill{{Collection: "users", ID: "u", SupplierID: "default"}, {Collection: "projectMembers", ID: "m", UserID: "u", SupplierID: "default"}}
|
||||
// The second update simulates an already-filled/deleted record: no retry
|
||||
// that drops the filter and no upsert may follow.
|
||||
mt.AddMockResponses(mtest.CreateSuccessResponse(bson.E{Key: "n", Value: 1}, bson.E{Key: "nModified", Value: 1}), mtest.CreateSuccessResponse(bson.E{Key: "n", Value: 0}, bson.E{Key: "nModified", Value: 0}))
|
||||
if err := ds.applyEmployeeSupplierBackfill(mt.Context(), "org-a", changes); err != nil {
|
||||
mt.Fatal(err)
|
||||
}
|
||||
events := mt.GetAllStartedEvents()
|
||||
if len(events) != 2 {
|
||||
mt.Fatalf("unexpected writes: %d", len(events))
|
||||
}
|
||||
for i, event := range events {
|
||||
var command struct {
|
||||
Update string `bson:"update"`
|
||||
Updates []struct {
|
||||
Q bson.M `bson:"q"`
|
||||
U bson.M `bson:"u"`
|
||||
Upsert bool `bson:"upsert"`
|
||||
} `bson:"updates"`
|
||||
}
|
||||
if err := bson.Unmarshal(event.Command, &command); err != nil {
|
||||
mt.Fatal(err)
|
||||
}
|
||||
if command.Update != changes[i].Collection || len(command.Updates) != 1 || command.Updates[0].Upsert {
|
||||
mt.Fatal("wrong target/upsert")
|
||||
}
|
||||
u := command.Updates[0]
|
||||
if !reflect.DeepEqual(u.Q, employeeSupplierBackfillFilter("org-a", changes[i])) {
|
||||
mt.Fatalf("unsafe filter %#v", u.Q)
|
||||
}
|
||||
fields := u.U["$set"].(bson.M)
|
||||
if len(u.U) != 1 || len(fields) != 2 || fields["supplierId"] != "default" || fields["updatedAt"] == nil {
|
||||
mt.Fatalf("unsafe fields %#v", u.U)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@ -60,7 +60,7 @@ func (store *MongoStore) RunMigrations(ctx context.Context) error {
|
||||
return err
|
||||
}
|
||||
for _, organization := range organizations {
|
||||
if _, err := store.EnsureDefaultSupplier(ctx, organization.ID); err != nil {
|
||||
if err := store.backfillMissingEmployeeSuppliers(ctx, organization.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := store.SyncDefaultSupplierProjectMembers(ctx, organization.ID, "", ""); err != nil {
|
||||
|
||||
@ -20,9 +20,6 @@ def build_plan(inventory):
|
||||
continue
|
||||
mine = [m for m in members if m.get("userId") == user["id"]]
|
||||
supplier_id = (user.get("supplierId") or "").strip()
|
||||
if not supplier_id and any(m.get("supplierId") for m in mine):
|
||||
plan["conflicts"].append({"user": identity, "reason": "账号无供应商,但项目成员已有供应商;不猜测归属", "members": [{"id": m["id"], "projectId": m["projectId"], "supplierId": m.get("supplierId", "")} for m in mine]})
|
||||
continue
|
||||
if supplier_id and (supplier_id not in suppliers or suppliers[supplier_id].get("status") != "active"):
|
||||
plan["conflicts"].append({"user": identity, "reason": "现有供应商不存在、不属于本组织或不可用", "supplierId": supplier_id})
|
||||
continue
|
||||
|
||||
@ -29,6 +29,8 @@ class EmployeeSupplierPlanTest(unittest.TestCase):
|
||||
def test_conflicts_and_other_organizations_are_not_rewritten(self):
|
||||
snapshot = {"organizationId": "o", "users": [{"id": "u", "organizationId": "o", "role": "annotator"}, {"id": "admin", "organizationId": "o", "role": "admin"}, {"id": "outside", "organizationId": "other", "role": "annotator"}], "members": [{"id": "m", "organizationId": "o", "projectId": "p", "userId": "u", "supplierId": "s"}]}
|
||||
plan = module.build_plan(snapshot)
|
||||
self.assertEqual(plan["changes"], [])
|
||||
self.assertEqual(len(plan["changes"]), 1)
|
||||
self.assertEqual(plan["changes"][0]["collection"], "users")
|
||||
self.assertEqual(plan["changes"][0]["afterSupplierId"], "supplier-default-o")
|
||||
self.assertEqual(len(plan["conflicts"]), 1)
|
||||
self.assertEqual(len(plan["excludedUsers"]), 1)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user