upsertGuardian must be called after setting the namespace in the context.
(ctx context.Context)
| 460 | |
| 461 | // upsertGuardian must be called after setting the namespace in the context. |
| 462 | func upsertGuardian(ctx context.Context) error { |
| 463 | query := fmt.Sprintf(` |
| 464 | { |
| 465 | guid as guardians(func: eq(dgraph.xid, "%s")) @filter(type(dgraph.type.Group)) { |
| 466 | uid |
| 467 | } |
| 468 | } |
| 469 | `, x.SuperAdminId) |
| 470 | groupNQuads := acl.CreateGroupNQuads(x.SuperAdminId) |
| 471 | req := &Request{ |
| 472 | req: &api.Request{ |
| 473 | CommitNow: true, |
| 474 | Query: query, |
| 475 | Mutations: []*api.Mutation{ |
| 476 | { |
| 477 | Set: groupNQuads, |
| 478 | Cond: "@if(eq(len(guid), 0))", |
| 479 | }, |
| 480 | }, |
| 481 | }, |
| 482 | doAuth: NoAuthorize, |
| 483 | } |
| 484 | |
| 485 | resp, err := (&Server{}).doQuery(ctx, req) |
| 486 | |
| 487 | // Structs to parse guardians group uid from query response |
| 488 | type groupNode struct { |
| 489 | Uid string `json:"uid"` |
| 490 | } |
| 491 | |
| 492 | type groupQryResp struct { |
| 493 | GuardiansGroup []groupNode `json:"guardians"` |
| 494 | } |
| 495 | |
| 496 | if err != nil { |
| 497 | return errors.Wrapf(err, "while upserting group with id %s", x.SuperAdminId) |
| 498 | } |
| 499 | var groupResp groupQryResp |
| 500 | var guardiansUidStr string |
| 501 | if err := json.Unmarshal(resp.GetJson(), &groupResp); err != nil { |
| 502 | return errors.Wrap(err, "Couldn't unmarshal response from guardians group query") |
| 503 | } |
| 504 | |
| 505 | if len(groupResp.GuardiansGroup) == 0 { |
| 506 | // no guardians group found |
| 507 | // Extract guardians group uid from mutation |
| 508 | newGroupUidMap := resp.GetUids() |
| 509 | guardiansUidStr = newGroupUidMap["newgroup"] |
| 510 | } else if len(groupResp.GuardiansGroup) == 1 { |
| 511 | // we found a guardians group |
| 512 | guardiansUidStr = groupResp.GuardiansGroup[0].Uid |
| 513 | } else { |
| 514 | return errors.Wrap(err, "Multiple guardians group found") |
| 515 | } |
| 516 | |
| 517 | uid, err := strconv.ParseUint(guardiansUidStr, 0, 64) |
| 518 | if err != nil { |
| 519 | return errors.Wrapf(err, "Error while parsing Uid: %s of guardians Group", guardiansUidStr) |
no test coverage detected