Skip to content

Commit

Permalink
feat: implement community rules validator
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Jan 31, 2024
1 parent 51d2690 commit 9f03198
Show file tree
Hide file tree
Showing 25 changed files with 723 additions and 125 deletions.
2 changes: 2 additions & 0 deletions api-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ type Mutation {
userUpdateRuleCondition(input: UserUpdateRuleConditionInput!, ruleConditionId: String!): RuleCondition
userUpdateUser(input: UserUpdateUserInput!): User
userValidateRule(address: String!, ruleId: String!): [RuleCondition!]
userValidateRules(communityId: String!): JSON
userVerifyIdentityChallenge(input: VerifyIdentityChallengeInput!): IdentityChallenge
}

Expand All @@ -404,6 +405,7 @@ type Network {
type NetworkAsset {
accounts: [String!]!
amount: String!
group: String
owner: String!
}

Expand Down
25 changes: 17 additions & 8 deletions libs/api/backup/data-access/src/lib/api-backup.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,31 @@ export class ApiBackupService {
async restoreBackup(name: string) {
const backup = await this.readBackupFile(name)

const userIds = await this.core.data.user
.findMany({ select: { id: true } })
.then((users) => users.map((user) => user.id))
const [usernames, userIds] = await Promise.all([
this.core.data.user.findMany({ select: { username: true } }).then((users) => users.map((user) => user.username)),
this.core.data.user.findMany({ select: { id: true } }).then((users) => users.map((user) => user.id)),
])

const toCreate = backup.data.users.filter((user: { id: string }) => !userIds.includes(user.id))
const toCreate = backup.data.users.filter((user: { id: string; username: string }) => {
return !userIds.includes(user.id) && !usernames.includes(user.username)
})
if (!toCreate.length) {
this.logger.verbose(`No new users to create`)
return true
}
for (const user of toCreate) {
const { identities, ...userData } = user
const newUser = await this.core.data.user.create({
data: { ...userData, identities: { create: identities } },
})
try {
const newUser = await this.core.data.user.create({
data: { ...userData, identities: { create: identities } },
})

this.logger.verbose(`Created user ${newUser.username} with id ${newUser.id} and ${identities.length} identities`)
this.logger.verbose(
`Created user ${newUser.username} with id ${newUser.id} and ${identities.length} identities`,
)
} catch (error) {
this.logger.error(`Failed to create user ${user.username}: ${error}`)
}
}
return true
}
Expand Down
1 change: 1 addition & 0 deletions libs/api/community-member/data-access/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ export * from './lib/dto/user-create-community-member.input'
export * from './lib/dto/user-find-many-community-member.input'
export * from './lib/dto/user-update-community-member.input'
export * from './lib/entity/community-member-paging.entity'
export * from './lib/entity/community-member-rule.entity'
export * from './lib/entity/community-member.entity'
export * from './lib/entity/community-role.enum'
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Field, ObjectType } from '@nestjs/graphql'
import { Rule } from '@pubkey-link/api-rule-data-access'
import { CommunityMember } from './community-member.entity'

@ObjectType()
export class CommunityMemberRule {
@Field()
id!: string
@Field({ nullable: true })
createdAt?: Date
@Field({ nullable: true })
updatedAt?: Date
@Field()
memberId!: string
@Field({ nullable: true })
member?: CommunityMember
@Field()
ruleId!: string
@Field({ nullable: true })
rule?: Rule
}
22 changes: 21 additions & 1 deletion libs/api/community/data-access/src/lib/api-community.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,28 @@
import { Injectable } from '@nestjs/common'
import { CommunityRole } from '@prisma/client'
import { ApiCoreService } from '@pubkey-link/api-core-data-access'
import { ApiAdminCommunityService } from './api-admin-community.service'
import { ApiUserCommunityService } from './api-user-community.service'

@Injectable()
export class ApiCommunityService {
constructor(readonly admin: ApiAdminCommunityService, readonly user: ApiUserCommunityService) {}
constructor(
readonly core: ApiCoreService,
readonly admin: ApiAdminCommunityService,
readonly user: ApiUserCommunityService,
) {}

async ensureCommunityAdmin(userId: string, communityId: string) {
const found = await this.core.data.community.findUnique({
where: { id: communityId },
include: { members: { where: { userId, role: CommunityRole.Admin } } },
})
if (!found) {
throw new Error(`Community ${communityId} not found`)
}
if (!found.members.length) {
throw new Error(`User ${userId} is not an admin of community ${communityId}`)
}
return found
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { NetworkCluster } from '@pubkey-link/api-network-data-access'
@InputType()
export class AdminCreateCommunityInput {
@Field(() => NetworkCluster)
cluster: NetworkCluster
cluster!: NetworkCluster
@Field()
name!: string
@Field({ nullable: true })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { NetworkCluster } from '@pubkey-link/api-network-data-access'
@InputType()
export class UserCreateCommunityInput {
@Field(() => NetworkCluster)
cluster: NetworkCluster
cluster!: NetworkCluster
@Field()
name!: string
@Field({ nullable: true })
Expand Down
Loading

0 comments on commit 9f03198

Please sign in to comment.