Просмотр исходного кода

feat(server): Create roles API resolver & service methods

Michael Bromley 7 лет назад
Родитель
Сommit
5afa6b2db7

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
schema.json


+ 2 - 0
server/src/api/api.module.ts

@@ -16,6 +16,7 @@ import { GraphqlConfigService } from './graphql-config.service';
 import { JwtStrategy } from './jwt.strategy';
 import { ProductOptionResolver } from './product-option/product-option.resolver';
 import { ProductResolver } from './product/product.resolver';
+import { RoleResolver } from './role/role.resolver';
 
 const exportedProviders = [
     AdministratorResolver,
@@ -26,6 +27,7 @@ const exportedProviders = [
     CustomerResolver,
     ProductOptionResolver,
     ProductResolver,
+    RoleResolver,
 ];
 
 /**

+ 36 - 0
server/src/api/role/role.api.graphql

@@ -0,0 +1,36 @@
+type Query {
+  roles(options: RoleListOptions): RoleList!
+  role(id: ID!): Role
+}
+
+type Mutation {
+  "Create a new Role"
+  createRole(input: CreateRoleInput!): Role!
+}
+
+type RoleList implements PaginatedList {
+    items: [Role!]!
+    totalItems: Int!
+}
+
+input RoleListOptions {
+    take: Int
+    skip: Int
+    sort: RoleSortParameter
+    filter: RoleFilterParameter
+}
+
+input RoleSortParameter {
+    id: SortOrder
+    createdAt: SortOrder
+    updatedAt: SortOrder
+    code: SortOrder
+    description: SortOrder
+}
+
+input RoleFilterParameter {
+    code: StringOperators
+    description: StringOperators
+    createdAt: DateOperators
+    updatedAt: DateOperators
+}

+ 35 - 0
server/src/api/role/role.resolver.ts

@@ -0,0 +1,35 @@
+import { Args, Mutation, Query, Resolver } from '@nestjs/graphql';
+import { PaginatedList } from 'shared/shared-types';
+
+import { Permission } from '../../entity/role/permission';
+import { Role } from '../../entity/role/role.entity';
+import { RoleService } from '../../service/role.service';
+import { ApplyIdCodec } from '../common/apply-id-codec-decorator';
+import { RolesGuard } from '../roles-guard';
+
+@Resolver('Roles')
+export class RoleResolver {
+    constructor(private roleService: RoleService) {}
+
+    @Query()
+    @RolesGuard([Permission.ReadAdministrator])
+    @ApplyIdCodec()
+    roles(@Args() args: any): Promise<PaginatedList<Role>> {
+        return this.roleService.findAll(args.options);
+    }
+
+    @Query()
+    @RolesGuard([Permission.ReadAdministrator])
+    @ApplyIdCodec()
+    role(@Args() args: any): Promise<Role | undefined> {
+        return this.roleService.findOne(args.id);
+    }
+
+    @Mutation()
+    @RolesGuard([Permission.CreateAdministrator])
+    @ApplyIdCodec()
+    createRole(_, args): Promise<Role> {
+        const { input } = args;
+        return this.roleService.create(input);
+    }
+}

+ 8 - 1
server/src/entity/role/role.graphql

@@ -1,6 +1,13 @@
-type Role {
+type Role implements Node {
+    id: ID!
     code: String!
     description: String!
     permissions: [String!]!
     channels: [Channel!]!
 }
+
+input CreateRoleInput {
+    code: String!
+    description: String!
+    permissions: [String!]!
+}

+ 6 - 8
server/src/service/administrator.service.ts

@@ -1,6 +1,6 @@
 import { Injectable } from '@nestjs/common';
 import { InjectConnection } from '@nestjs/typeorm';
-import { PaginatedList } from 'shared/shared-types';
+import { ID, PaginatedList } from 'shared/shared-types';
 import { Connection } from 'typeorm';
 
 import { ListQueryOptions } from '../common/types/common-types';
@@ -23,15 +23,13 @@ export class AdministratorService {
     findAll(options: ListQueryOptions<Administrator>): Promise<PaginatedList<Administrator>> {
         return buildListQuery(this.connection, Administrator, options, ['user', 'user.roles'])
             .getManyAndCount()
-            .then(([items, totalItems]) => {
-                return {
-                    items,
-                    totalItems,
-                };
-            });
+            .then(([items, totalItems]) => ({
+                items,
+                totalItems,
+            }));
     }
 
-    findOne(administratorId: string): Promise<Administrator | undefined> {
+    findOne(administratorId: ID): Promise<Administrator | undefined> {
         return this.connection.manager.findOne(Administrator, administratorId);
     }
 

+ 16 - 1
server/src/service/role.service.ts

@@ -1,5 +1,5 @@
 import { Injectable } from '@nestjs/common';
-import { InjectConnection } from '@nestjs/typeorm';
+import { ID, PaginatedList } from 'shared/shared-types';
 import { Connection } from 'typeorm';
 
 import {
@@ -8,11 +8,13 @@ import {
     SUPER_ADMIN_ROLE_CODE,
     SUPER_ADMIN_ROLE_DESCRIPTION,
 } from '../common/constants';
+import { ListQueryOptions } from '../common/types/common-types';
 import { Permission } from '../entity/role/permission';
 import { Role } from '../entity/role/role.entity';
 import { I18nError } from '../i18n/i18n-error';
 
 import { ChannelService } from './channel.service';
+import { buildListQuery } from './helpers/build-list-query';
 import { ActiveConnection } from './helpers/connection.decorator';
 
 export interface CreateRoleDto {
@@ -30,6 +32,19 @@ export class RoleService {
         await this.ensureCustomerRoleExists();
     }
 
+    findAll(options: ListQueryOptions<Role>): Promise<PaginatedList<Role>> {
+        return buildListQuery(this.connection, Role, options)
+            .getManyAndCount()
+            .then(([items, totalItems]) => ({
+                items,
+                totalItems,
+            }));
+    }
+
+    findOne(roleId: ID): Promise<Role | undefined> {
+        return this.connection.manager.findOne(Role, roleId);
+    }
+
     getSuperAdminRole(): Promise<Role> {
         return this.getRoleByCode(SUPER_ADMIN_ROLE_CODE).then(role => {
             if (!role) {

Некоторые файлы не были показаны из-за большого количества измененных файлов