소스 검색

Add `CandidacyStatus` to the `Candidate` entity

Theophile Sandoz 3 년 전
부모
커밋
5dff0814ec
2개의 변경된 파일48개의 추가작업 그리고 6개의 파일을 삭제
  1. 15 6
      query-node/mappings/src/council.ts
  2. 33 0
      query-node/schemas/council.graphql

+ 15 - 6
query-node/mappings/src/council.ts

@@ -48,6 +48,10 @@ import {
   ElectedCouncil,
   CastVote,
   CandidacyNoteMetadata,
+  CandidacyStatusActive,
+  CandidacyStatusWithdrawn,
+  CandidacyStatusElected,
+  CandidacyStatusLost,
 
   // Misc
   Membership,
@@ -387,7 +391,7 @@ export async function council_NewCandidate({ event, store }: EventContext & Stor
     stakingAccountId: stakingAccount.toString(),
     rewardAccountId: rewardAccount.toString(),
     member,
-
+    status: new CandidacyStatusActive(),
     electionRound,
     stake: balance,
     stakeLocked: true,
@@ -433,11 +437,15 @@ export async function council_NewCouncilElected({ event, store }: EventContext &
   // get election round and its candidates
   const electionRound = await getCurrentElectionRound(store)
 
-  // TODO: uncomment when following query will be working (after some QN patches make it to Olympia)
-  // const electedCandidates = await store.getMany(Candidate, { where: { electionRoundId: electionRound.id, member: { id_in: electedMemberIds } } })
-  const electedCandidates = (
-    await store.getMany(Candidate, { where: { electionRoundId: electionRound.id }, relations: ['member'] })
-  ).filter((item: Candidate) => electedMemberIds.find((tmpId) => tmpId === item.member.id.toString()))
+  const where = { electionRoundId: electionRound.id, status_json: { isTypeOf_eq: 'CandidacyStatusActive' } }
+  const candidates = await store.getMany(Candidate, { where })
+
+  const electedCandidates = candidates.filter((candidate) => electedMemberIds.includes(candidate.member.id))
+
+  // Set candidates elected status
+  electedCandidates.forEach((candidate) => {
+    candidate.status = new CandidacyStatusElected()
+  })
 
   // create new council record
   const electedCouncil = new ElectedCouncil({
@@ -561,6 +569,7 @@ export async function council_CandidacyWithdraw({ event, store }: EventContext &
 
   // mark candidacy as withdrawn
   candidate.candidacyWithdrawn = true
+  candidate.status = new CandidacyStatusWithdrawn()
   await store.save<Candidate>(candidate)
 }
 

+ 33 - 0
query-node/schemas/council.graphql

@@ -46,6 +46,36 @@ enum ElectionProblem {
   NEW_COUNCIL_NOT_ELECTED
 }
 
+type CandidacyStatusActive @variant {
+  # no properties
+
+  # TODO: remove me - variant needs to have at least 1 property now
+  dummy: Int
+}
+
+type CandidacyStatusWithdrawn @variant {
+  # no properties
+
+  # TODO: remove me - variant needs to have at least 1 property now
+  dummy: Int
+}
+
+type CandidacyStatusElected @variant {
+  # no properties
+
+  # TODO: remove me - variant needs to have at least 1 property now
+  dummy: Int
+}
+
+type CandidacyStatusLost @variant {
+  # no properties
+
+  # TODO: remove me - variant needs to have at least 1 property now
+  dummy: Int
+}
+
+union CandidacyStatus = CandidacyStatusActive | CandidacyStatusElected | CandidacyStatusLost
+
 type Candidate @entity {
   "Account used for staking currency needed for the candidacy."
   stakingAccountId: String!
@@ -65,6 +95,9 @@ type Candidate @entity {
   "Reflects if the stake is still locked for candidacy or has been already released by the member."
   stakeLocked: Boolean!
 
+  "Current candidate status"
+  status: CandidacyStatus!
+
   "Reflects if the candidacy was withdrawn before voting started."
   candidacyWithdrawn: Boolean!