diff --git a/src/datasources/db/actionLogAPI.ts b/src/datasources/db/actionLogAPI.ts new file mode 100644 index 0000000..e44726e --- /dev/null +++ b/src/datasources/db/actionLogAPI.ts @@ -0,0 +1,28 @@ +import { DataSource } from 'apollo-datasource'; +import { Connection, getConnection } from 'typeorm'; +import { ActionLog } from '../../model/ActionLog'; + +export class ActionLogAPI extends DataSource { + connection : Connection + constructor () { + super(); + this.connection = getConnection(); + } + + async actionLogByUserId (userId: number) { + return await this.connection.getRepository(ActionLog) + .createQueryBuilder('al') + .select() + .where('"userId" = :uid', { uid: userId }) + .orderBy('date', 'DESC') + .getMany(); + } + + async actionLogAll () { + return await this.connection.getRepository(ActionLog) + .createQueryBuilder('al') + .select() + .orderBy('date', 'DESC') + .getMany(); + } +} diff --git a/src/datasources/userserver/permission.ts b/src/datasources/userserver/permission.ts index d58c949..e25a030 100644 --- a/src/datasources/userserver/permission.ts +++ b/src/datasources/userserver/permission.ts @@ -25,7 +25,9 @@ export enum Permission { WriteWorkshopType = 'WORKSHOP_TYPE_WRITE', WriteEventType = 'BIKE_EVENT_TYPE_WRITE', WriteEquipmentType = 'EQUIPMENT_TYPE_WRITE', - WriteEngagementType = 'ENGAGEMENT_TYPE_WRITE' + WriteEngagementType = 'ENGAGEMENT_TYPE_WRITE', + ReadActionLog = 'ACTION_LOG_READ', + ReadActionLogAll = 'ACTION_LOG_ALL_READ' } // Permissions where the creation will be requested on startup @@ -133,5 +135,13 @@ export const requiredPermissions = [ { name: Permission.WriteEngagementType, description: 'Allows to write and create engagement types' + }, + { + name: Permission.ReadActionLog, + description: 'Allows to read own action log' + }, + { + name: Permission.ReadActionLogAll, + description: 'Allows to read action log of other users' } ]; diff --git a/src/index.ts b/src/index.ts index 3de3fa1..0b305d5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -34,6 +34,8 @@ import { BikeEventType } from './model/BikeEventType'; import { WorkshopAPI } from './datasources/db/workshopAPI'; import workshopResolvers from './resolvers/workshopResolvers'; import { ActionLog } from './model/ActionLog'; +import actionlogResolvers from './resolvers/actionlogResolvers'; +import { ActionLogAPI } from './datasources/db/actionLogAPI'; require('dotenv').config(); @@ -103,7 +105,8 @@ const server = new ApolloServer({ participantResolvers, providerResolvers, contactinformationResolvers, - workshopResolvers + workshopResolvers, + actionlogResolvers ], typeDefs, dataSources: () => ({ @@ -113,6 +116,7 @@ const server = new ApolloServer({ contactInformationAPI: new ContactInformationAPI(), providerAPI: new ProviderAPI(), workshopAPI: new WorkshopAPI(), + actionLogAPI: new ActionLogAPI(), userAPI }), context: (req: any) => { diff --git a/src/resolvers/actionlogResolvers.ts b/src/resolvers/actionlogResolvers.ts new file mode 100644 index 0000000..5161271 --- /dev/null +++ b/src/resolvers/actionlogResolvers.ts @@ -0,0 +1,28 @@ +import { Permission } from '../datasources/userserver/permission'; +import { GraphQLError } from 'graphql'; + +export default { + Query: { + actionLog: (_: any, __:any, { dataSources, req }:{dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadActionLog)) { + return dataSources.actionLogAPI.actionLogByUserId(req.userId); + } else { + return new GraphQLError('Insufficient Permissions'); + } + }, + actionLogByUser: (_: any, { id }: {id: number}, { dataSources, req }:{dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadActionLogAll)) { + return dataSources.actionLogAPI.actionLogByUserId(id); + } else { + return new GraphQLError('Insufficient Permissions'); + } + }, + actionLogAll: (_: any, __: any, { dataSources, req }:{dataSources: any, req: any }) => { + if (req.permissions.includes(Permission.ReadActionLogAll)) { + return dataSources.actionLogAPI.actionLogAll(); + } else { + return new GraphQLError('Insufficient Permissions'); + } + } + } +}; diff --git a/src/schema/type-defs.ts b/src/schema/type-defs.ts index 21f9524..d2e153e 100644 --- a/src/schema/type-defs.ts +++ b/src/schema/type-defs.ts @@ -865,6 +865,17 @@ input AddressUpdateInput { zip: String } +type ActionLog { + id: ID! + userId: ID! + date: Date! + entity: String! + "in json format" + entriesOld: String! + "in json format" + entriesNew: String! +} + type Query { "Will (eventually) return all properties of cargo bike" cargoBikeById(id:ID!): CargoBike @@ -904,6 +915,12 @@ type Query { bikeEventTypeByd(id: ID!): BikeEventType bikeEvents(offset: Int!, limit: Int!): [BikeEvent]! bikeEventById(id:ID!): BikeEvent + "actionLog for current user" + actionLog: [ActionLog] + "actionLog for specific user" + actionLogByUser(id: ID!): [ActionLog] + "actionLog form all users" + actionLogAll: [ActionLog] } type Mutation {