Skip to content

Commit

Permalink
refactor: improve logging output
Browse files Browse the repository at this point in the history
  • Loading branch information
beeman committed Nov 5, 2024
1 parent 740992c commit 3dc74f8
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export class ApiCommunityDataService {
async create(userId: string, input: Prisma.CommunityCreateInput) {
this.core.config.ensureFeature(AppFeature.CommunityCreate)
await this.core.ensureNetworkCluster(input.cluster)
return this.core.createCommunity({ userId, input })
const created = await this.core.createCommunity({ userId, input })
await this.core.logInfo(`[${created.id}] Community created`, { userId, communityId: created.id })
return created
}

async delete(communityId: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,22 @@ export class ApiNetworkTokenDataService {

private async ensureGenesisBlock() {
if (!this.core.config.featureResolverSolanaValidator) {
this.logger.debug(`ensureGenesisBlock: Feature resolverSolanaValidator disabled, skipping genesis block check.`)
this.logger.debug(
`[GLOBAL] ensureGenesisBlock: Feature resolverSolanaValidator disabled, skipping genesis block check.`,
)
return
}
this.logger.debug(`ensureGenesisBlock: Ensuring genesis block for validators.`)
this.logger.debug(`[GLOBAL] ensureGenesisBlock: Ensuring genesis block for validators.`)

const existing = await this.core.data.networkToken.findMany({ where: { type: NetworkTokenType.Validator } })

const networks = await this.core.data.network.findMany({
where: { cluster: { notIn: existing.map(({ cluster }) => cluster) } },
})

this.logger.verbose(`ensureGenesisBlock: Found ${existing.length} existing tokens, adding ${networks.length} more.`)
this.logger.verbose(
`[GLOBAL] ensureGenesisBlock: Found ${existing.length} existing tokens, adding ${networks.length} more.`,
)

for (const network of networks) {
this.logger.verbose(`[${network.cluster}] ensureGenesisBlock: Ensuring genesis block for cluster.`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { getRoleWhereAdminInput } from './helpers/get-role-where-admin.input'
export class ApiRoleDataAdminService {
constructor(private readonly data: ApiRoleDataService) {}

async createRole(input: AdminCreateRoleInput) {
return this.data.create(input)
async createRole(userId: string, input: AdminCreateRoleInput) {
return this.data.create(userId, input)
}

async deleteRole(roleId: string) {
return this.data.delete(roleId)
async deleteRole(userId: string, roleId: string) {
return this.data.delete(userId, roleId)
}

async findManyRole(input: AdminFindManyRoleInput): Promise<RolePaging> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ export class ApiRoleDataUserService {

async createRole(userId: string, input: UserCreateRoleInput) {
await this.core.ensureCommunityAdmin({ communityId: input.communityId, userId })
return this.data.create(input)
return this.data.create(userId, input)
}

async deleteRole(userId: string, roleId: string) {
await this.data.ensureRoleAdmin({ userId, roleId })
return this.data.delete(roleId)
return this.data.delete(userId, roleId)
}

async findManyRole(userId: string, input: UserFindManyRoleInput): Promise<RolePaging> {
Expand Down Expand Up @@ -63,11 +63,13 @@ export class ApiRoleDataUserService {
await this.data.ensureRoleAdmin({ userId, roleId: input.roleId })
return this.condition.create(input)
}

async deleteRoleCondition(userId: string, roleConditionId: string) {
const found = await this.condition.findOne(roleConditionId)
await this.data.ensureRoleAdmin({ userId, roleId: found.roleId })
return this.condition.deleteRoleCondition(roleConditionId)
}

async updateRoleCondition(userId: string, roleConditionId: string, input: UserUpdateRoleConditionInput) {
const found = await this.condition.findOne(roleConditionId)
await this.data.ensureRoleAdmin({ userId, roleId: found.roleId })
Expand All @@ -78,6 +80,7 @@ export class ApiRoleDataUserService {
await this.data.ensureRoleAdmin({ userId, roleId: input.roleId })
return this.permission.create(input)
}

async deleteRolePermission(userId: string, rolePermissionId: string) {
const found = await this.permission.findOne(rolePermissionId)
await this.data.ensureRoleAdmin({ userId, roleId: found.roleId })
Expand Down
13 changes: 10 additions & 3 deletions libs/api/role/data-access/src/lib/api-role-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@ import { RolePaging } from './entity/role.entity'
export class ApiRoleDataService {
constructor(private readonly core: ApiCoreService) {}

async create(input: Prisma.RoleUncheckedCreateInput) {
return this.core.data.role.create({ data: input })
async create(userId: string, input: Prisma.RoleUncheckedCreateInput) {
const created = await this.core.data.role.create({ data: input })
await this.core.logInfo(`[${created.name}] Role created`, {
roleId: created.id,
communityId: created.communityId,
userId,
})
return created
}

async delete(roleId: string) {
async delete(userId: string, roleId: string) {
await this.findOne(roleId)
const deleted = await this.core.data.role.delete({ where: { id: roleId } })
await this.core.logInfo(`[${roleId}] Role deleted`, { communityId: deleted.communityId, userId })
return !!deleted
}

Expand Down
10 changes: 5 additions & 5 deletions libs/api/role/data-access/src/lib/api-role-resolver.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ export class ApiRoleResolverService {
where: { enableSync: true },
})
if (!communities.filter((c) => c.enableSync).length) {
this.logger.warn(`syncAllCommunityRoles: No communities found with enableSync=true`)
this.logger.warn(`[GLOBAL] syncAllCommunityRoles: No communities found with enableSync=true`)
return
}
this.logger.verbose(`Validating roles of ${communities.length} communities`)
this.logger.verbose(`[GLOBAL] Validating roles of ${communities.length} communities`)
for (const community of communities) {
await this.syncCommunityRoles(community.id)
}
Expand All @@ -50,7 +50,7 @@ export class ApiRoleResolverService {
async syncCommunityRoles(communityId: string) {
const community = await this.core.data.community.findUnique({ where: { id: communityId } })
if (!community) {
throw new Error(`Community not found`)
throw new Error(`[${communityId}] Community not found`)
}
const startedAt = Date.now()

Expand All @@ -72,10 +72,10 @@ export class ApiRoleResolverService {
}

if (!conditions?.length || !users?.length) {
this.logger.debug(`syncCommunityRoles: No conditions or users found for community ${communityId}`)
this.logger.debug(`[${community.id}] syncCommunityRoles: No conditions or users found for community`)
return result
}
this.logger.verbose(`Validating ${conditions.length} conditions for ${users.length} users`)
this.logger.verbose(`[${community.id}] Validating ${conditions.length} conditions for ${users.length} users`)

for (const user of users) {
// We are now in the context of a user
Expand Down
17 changes: 8 additions & 9 deletions libs/api/role/feature/src/lib/api-admin-role.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Resolver } from '@nestjs/graphql'
import { ApiRoleService } from '@pubkey-link/api-role-data-access'
import { ApiAuthGraphQLAdminGuard } from '@pubkey-link/api-auth-data-access'
import { Mutation, Query, Args } from '@nestjs/graphql'
import { UseGuards } from '@nestjs/common'
import { Args, Mutation, Query, Resolver } from '@nestjs/graphql'
import { ApiAuthGraphQLAdminGuard, CtxUserId } from '@pubkey-link/api-auth-data-access'
import {
AdminCreateRoleInput,
AdminFindManyRoleInput,
AdminUpdateRoleInput,
ApiRoleService,
Role,
RolePaging,
AdminUpdateRoleInput,
} from '@pubkey-link/api-role-data-access'

@Resolver()
Expand All @@ -17,13 +16,13 @@ export class ApiAdminRoleResolver {
constructor(private readonly service: ApiRoleService) {}

@Mutation(() => Role, { nullable: true })
adminCreateRole(@Args('input') input: AdminCreateRoleInput) {
return this.service.admin.createRole(input)
adminCreateRole(@CtxUserId() userId: string, @Args('input') input: AdminCreateRoleInput) {
return this.service.admin.createRole(userId, input)
}

@Mutation(() => Boolean, { nullable: true })
adminDeleteRole(@Args('roleId') roleId: string) {
return this.service.admin.deleteRole(roleId)
adminDeleteRole(@CtxUserId() userId: string, @Args('roleId') roleId: string) {
return this.service.admin.deleteRole(userId, roleId)
}

@Query(() => RolePaging)
Expand Down
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ model Log {
relatedType LogRelatedType?
community Community? @relation(fields: [communityId], references: [id], onDelete: Cascade)
communityId String?
identity Identity? @relation(fields: [identityProvider, identityProviderId], references: [provider, providerId], onDelete: Cascade)
identity Identity? @relation(fields: [identityProvider, identityProviderId], references: [provider, providerId], onDelete: SetNull)
identityProvider IdentityProvider?
identityProviderId String?
bot Bot? @relation(fields: [botId], references: [id], onDelete: Cascade)
Expand Down

0 comments on commit 3dc74f8

Please sign in to comment.