Skip to content

Commit

Permalink
feat: add option to add condition types
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Jan 31, 2024
1 parent 93ca20f commit 51d2690
Show file tree
Hide file tree
Showing 43 changed files with 1,036 additions and 154 deletions.
21 changes: 21 additions & 0 deletions api-schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,14 @@ type Mutation {
userCreateCommunity(input: UserCreateCommunityInput!): Community
userCreateCommunityMember(input: UserCreateCommunityMemberInput!): CommunityMember
userCreateRule(input: UserCreateRuleInput!): Rule
userCreateRuleCondition(input: UserCreateRuleConditionInput!): RuleCondition
userCreateRulePermission(input: UserCreateRulePermissionInput!): RulePermission
userDeleteBot(botId: String!): Boolean
userDeleteCommunity(communityId: String!): Boolean
userDeleteCommunityMember(communityMemberId: String!): Boolean
userDeleteIdentity(identityId: String!): Boolean
userDeleteRule(ruleId: String!): Boolean
userDeleteRuleCondition(ruleConditionId: String!): Boolean
userDeleteRulePermission(rulePermissionId: String!): Boolean
userLeaveBotServer(botId: String!, serverId: String!): Boolean
userLinkIdentity(input: LinkIdentityInput!): Identity
Expand All @@ -380,6 +382,7 @@ type Mutation {
userUpdateCommunity(communityId: String!, input: UserUpdateCommunityInput!): Community
userUpdateCommunityMember(communityMemberId: String!, input: UserUpdateCommunityMemberInput!): CommunityMember
userUpdateRule(input: UserUpdateRuleInput!, ruleId: String!): Rule
userUpdateRuleCondition(input: UserUpdateRuleConditionInput!, ruleConditionId: String!): RuleCondition
userUpdateUser(input: UserUpdateUserInput!): User
userValidateRule(address: String!, ruleId: String!): [RuleCondition!]
userVerifyIdentityChallenge(input: VerifyIdentityChallengeInput!): IdentityChallenge
Expand Down Expand Up @@ -597,6 +600,16 @@ input UserCreateCommunityMemberInput {
userId: String!
}

input UserCreateRuleConditionInput {
account: String
amount: String
config: JSON
filters: JSON
ruleId: String!
tokenId: String
type: RuleConditionType!
}

input UserCreateRuleInput {
communityId: String!
description: String
Expand Down Expand Up @@ -688,6 +701,14 @@ input UserUpdateCommunityMemberInput {
role: CommunityRole!
}

input UserUpdateRuleConditionInput {
account: String
amount: String
config: JSON
filters: JSON
tokenId: String
}

input UserUpdateRuleInput {
description: String
name: String
Expand Down
4 changes: 3 additions & 1 deletion libs/api/rule/data-access/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
export * from './lib/dto/user-update-rule-condition-input'
export * from './lib/api-rule-data-access.module'
export * from './lib/api-rule.service'
export * from './lib/dto/admin-create-rule.input'
export * from './lib/dto/admin-find-many-rule.input'
export * from './lib/dto/admin-update-rule.input'
export * from './lib/dto/user-create-rule.input'
export * from './lib/dto/user-create-rule-condition.input'
export * from './lib/dto/user-create-rule-permission.input'
export * from './lib/dto/user-create-rule.input'
export * from './lib/dto/user-find-many-rule.input'
export * from './lib/dto/user-update-rule.input'
export * from './lib/entity/rule-condition-type.enum'
Expand Down
40 changes: 40 additions & 0 deletions libs/api/rule/data-access/src/lib/api-user-rule.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Injectable } from '@nestjs/common'
import { ApiCoreService } from '@pubkey-link/api-core-data-access'
import { ApiRuleResolverService } from './api-rule-resolver.service'
import { UserCreateRuleConditionInput } from './dto/user-create-rule-condition.input'
import { UserCreateRulePermissionInput } from './dto/user-create-rule-permission.input'
import { UserCreateRuleInput } from './dto/user-create-rule.input'
import { UserFindManyRuleInput } from './dto/user-find-many-rule.input'
import { UserUpdateRuleConditionInput } from './dto/user-update-rule-condition-input'
import { UserUpdateRuleInput } from './dto/user-update-rule.input'
import { RuleCondition } from './entity/rule-condition.entity'
import { RulePaging } from './entity/rule-paging.entity'
Expand All @@ -16,6 +18,22 @@ export class ApiUserRuleService {
async createRule(input: UserCreateRuleInput) {
return this.core.data.rule.create({ data: input })
}

async createRuleCondition(input: UserCreateRuleConditionInput) {
return this.core.data.ruleCondition.create({
data: {
account: input.account,
amount: input.amount,
config: input.config ?? undefined,
filters: input.filters ?? undefined,
name: input.type.toString(),
rule: { connect: { id: input.ruleId } },
token: input.tokenId ? { connect: { id: input.tokenId } } : undefined,
type: input.type,
},
})
}

async createRulePermission(input: UserCreateRulePermissionInput) {
return this.core.data.rulePermission.create({
data: {
Expand All @@ -35,6 +53,17 @@ export class ApiUserRuleService {
return !!deleted
}

async deleteRuleCondition(ruleConditionId: string) {
const found = await this.core.data.ruleCondition.findUnique({
where: { id: ruleConditionId },
})
if (!found) {
throw new Error('Rule permission not found')
}
const deleted = await this.core.data.ruleCondition.delete({ where: { id: ruleConditionId } })
return !!deleted
}

async deleteRulePermission(rulePermissionId: string) {
const found = await this.core.data.rulePermission.findUnique({
where: { id: rulePermissionId },
Expand Down Expand Up @@ -72,6 +101,17 @@ export class ApiUserRuleService {
return this.core.data.rule.update({ where: { id: ruleId }, data: input })
}

async updateRuleCondition(ruleConditionId: string, input: UserUpdateRuleConditionInput) {
return this.core.data.ruleCondition.update({
where: { id: ruleConditionId },
data: {
...input,
config: input.config ?? undefined,
filters: input.filters ?? undefined,
},
})
}

async validateRule(ruleId: string, address: string): Promise<RuleCondition[]> {
const rule = await this.findOneRule(ruleId)
if (!rule) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Field, InputType } from '@nestjs/graphql'
import { Prisma } from '@prisma/client'
import { GraphQLJSON } from 'graphql-scalars'
import { RuleConditionType } from '../entity/rule-condition-type.enum'

@InputType()
export class UserCreateRuleConditionInput {
@Field()
ruleId!: string
@Field(() => RuleConditionType)
type!: RuleConditionType
@Field({ nullable: true })
tokenId?: string | null
@Field({ nullable: true })
account?: string | null
@Field({ nullable: true })
amount?: string | null
@Field(() => GraphQLJSON, { nullable: true })
config?: Prisma.InputJsonValue | null
@Field(() => GraphQLJSON, { nullable: true })
filters?: Prisma.InputJsonValue | null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Field, InputType } from '@nestjs/graphql'
import { Prisma } from '@prisma/client'
import { GraphQLJSON } from 'graphql-scalars'

@InputType()
export class UserUpdateRuleConditionInput {
@Field({ nullable: true })
tokenId?: string | null
@Field({ nullable: true })
account?: string | null
@Field({ nullable: true })
amount?: string | null
@Field(() => GraphQLJSON, { nullable: true })
config?: Prisma.InputJsonValue | null
@Field(() => GraphQLJSON, { nullable: true })
filters?: Prisma.InputJsonValue | null
}
20 changes: 20 additions & 0 deletions libs/api/rule/feature/src/lib/api-user-rule.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import {
RuleCondition,
RulePaging,
RulePermission,
UserCreateRuleConditionInput,
UserCreateRuleInput,
UserCreateRulePermissionInput,
UserFindManyRuleInput,
UserUpdateRuleConditionInput,
UserUpdateRuleInput,
} from '@pubkey-link/api-rule-data-access'

Expand All @@ -23,11 +25,21 @@ export class ApiUserRuleResolver {
return this.service.user.createRule(input)
}

@Mutation(() => RuleCondition, { nullable: true })
userCreateRuleCondition(@Args('input') input: UserCreateRuleConditionInput) {
return this.service.user.createRuleCondition(input)
}

@Mutation(() => RulePermission, { nullable: true })
userCreateRulePermission(@Args('input') input: UserCreateRulePermissionInput) {
return this.service.user.createRulePermission(input)
}

@Mutation(() => Boolean, { nullable: true })
userDeleteRuleCondition(@Args('ruleConditionId') ruleConditionId: string) {
return this.service.user.deleteRuleCondition(ruleConditionId)
}

@Mutation(() => Boolean, { nullable: true })
userDeleteRulePermission(@Args('rulePermissionId') rulePermissionId: string) {
return this.service.user.deleteRulePermission(rulePermissionId)
Expand Down Expand Up @@ -57,4 +69,12 @@ export class ApiUserRuleResolver {
userUpdateRule(@Args('ruleId') ruleId: string, @Args('input') input: UserUpdateRuleInput) {
return this.service.user.updateRule(ruleId, input)
}

@Mutation(() => RuleCondition, { nullable: true })
userUpdateRuleCondition(
@Args('ruleConditionId') ruleConditionId: string,
@Args('input') input: UserUpdateRuleConditionInput,
) {
return this.service.user.updateRuleCondition(ruleConditionId, input)
}
}
Loading

0 comments on commit 51d2690

Please sign in to comment.