2

enter image description here

How can I insert into orgnanization and make sure that each time a member is created as the Admin of this organization? I can't seem to make the correct mutation insert.

Below is the main Organization Table which has a many-to-many relationship to OrganizationMember table.

type Organization @table {
    id: UUID! @default(expr: "uuidV4()")
    slug: String! @col(dataType: "varchar(255)") @unique @index
    name: String! @col(dataType: "varchar(200)")
    logo: String
    createdAt: Date! @default(expr: "request.time")
    updatedAt: Date! @default(expr: "request.time")
    owner: User!
    members: OrganizationMember!
}

type OrganizationMember @table {
    userId: String! @index
    user: User!
    organizationId: UUID! @index
    organization: Organization!
    role: String! @col(dataType: "varchar(50)")
}

I want to be able to create a mutation in which in one call creates the organization and adds the owner of that organization as one of the OrganizationMember's with role "ADMIN".

1 Answer 1

0

I hope the following will help you resolve your problem. In my case, I set the user's default company, division, and location once the user was created.

I'm not an expert in this field, so please bear with any mistakes.

# Create a new User with a default company, division, and location.
# This mutation handles setting the isDefault flag correctly by unsetting previous defaults.
mutation CreateUserWithDetails(
  $code: String!
  $displayName: String!
  $email: String!
  $userType: Int!
  $phoneNumber: String
  $designationId: Int
  $designation: String
  $companyId: UUID!
  $divisionId: UUID!
  $locationId: UUID!
) @auth(level: USER) @transaction {
  # 1. Create the User
  userResponse: user_insert(
    data: {
      code: $code
      displayName: $displayName
      email: $email
      userType: $userType
      phoneNumber: $phoneNumber
      designationId: $designationId
      designation: $designation
      createdAt_expr: "request.time"
      createdBy: { id_expr: "request.auth.uid" }
    }
  )

  # 2. Unset previous default company, division, and location
  unsetDefaultCompany: userCompany_update(
    first: {
      where: {
        user: { id: { eq_expr: "response.userResponse.id" } }
        isDefault: { eq: true }
      }
    }
    data: { isDefault: false }
  )
  unsetDefaultDivision: userDivision_update(
    first: {
      where: {
        user: { id: { eq_expr: "response.userResponse.id" } }
        isDefault: { eq: true }
      }
    }
    data: { isDefault: false }
  )
  unsetDefaultLocation: userLocation_update(
    first: {
      where: {
        user: { id: { eq_expr: "response.userResponse.id" } }
        isDefault: { eq: true }
      }
    }
    data: { isDefault: false }
  )

  # 3. Insert/Update user-company, user-division, and user-location associations
  userCompany: userCompany_upsert(
    data: {
      user: { id_expr: "response.userResponse.id" }
      company: { id: $companyId }
      isDefault: true
      createdBy: { id_expr: "request.auth.uid" }
      createdAt_expr: "request.time"
    }
  )
  userDivision: userDivision_upsert(
    data: {
      user: { id_expr: "response.userResponse.id" }
      division: { id: $divisionId }
      isDefault: true
      createdBy: { id_expr: "request.auth.uid" }
      createdAt_expr: "request.time"
    }
  )
  userLocation: userLocation_upsert(
    data: {
      user: { id_expr: "response.userResponse.id" }
      location: { id: $locationId }
      isDefault: true
      createdBy: { id_expr: "request.auth.uid" }
      createdAt_expr: "request.time"
    }
  )
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.