src/*/actionlog*.ts: implemented queries for action log

pull/14/head
leonnicolas 4 years ago
parent 98437cc33a
commit 97ee273ae4
No known key found for this signature in database
GPG Key ID: 088D0743E2B65C07

@ -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();
}
}

@ -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'
}
];

@ -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) => {

@ -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');
}
}
}
};

@ -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 {

Loading…
Cancel
Save