Add ResourceLockedError class

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/29/head
trivernis 4 years ago
parent 38f5a8a1fe
commit cfaf2ef462
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -20,7 +20,6 @@ This file is part of fLotte-API-Server.
import { DataSource } from 'apollo-datasource'; import { DataSource } from 'apollo-datasource';
import { Connection, EntityManager, getConnection } from 'typeorm'; import { Connection, EntityManager, getConnection } from 'typeorm';
import { CargoBike } from '../../model/CargoBike'; import { CargoBike } from '../../model/CargoBike';
import { GraphQLError } from 'graphql';
import { BikeEvent } from '../../model/BikeEvent'; import { BikeEvent } from '../../model/BikeEvent';
import { Equipment } from '../../model/Equipment'; import { Equipment } from '../../model/Equipment';
import { Engagement } from '../../model/Engagement'; import { Engagement } from '../../model/Engagement';
@ -29,8 +28,8 @@ import { TimeFrame } from '../../model/TimeFrame';
import { ActionLogger, DBUtils, genBoxDimensions, LockUtils } from './utils'; import { ActionLogger, DBUtils, genBoxDimensions, LockUtils } from './utils';
import { EquipmentType } from '../../model/EquipmentType'; import { EquipmentType } from '../../model/EquipmentType';
import { BikeEventType } from '../../model/BikeEventType'; import { BikeEventType } from '../../model/BikeEventType';
import { UserInputError } from 'apollo-server-express';
import { Actions } from '../../model/ActionLog'; import { Actions } from '../../model/ActionLog';
import { ResourceLockedError } from '../../errors/ResourceLockedError';
/** /**
* extended datasource to feed resolvers with data about cargoBikes * extended datasource to feed resolvers with data about cargoBikes
@ -107,7 +106,7 @@ export class CargoBikeAPI extends DataSource {
await this.connection.transaction(async (entityManager: EntityManager) => { await this.connection.transaction(async (entityManager: EntityManager) => {
if (await LockUtils.isLocked(entityManager, CargoBike, 'cb', cargoBike.id, userId)) { if (await LockUtils.isLocked(entityManager, CargoBike, 'cb', cargoBike.id, userId)) {
throw new GraphQLError('CargoBike locked by other user'); throw new ResourceLockedError('CargoBike');
} }
await ActionLogger.log(entityManager, CargoBike, 'cb', cargoBike, userId); await ActionLogger.log(entityManager, CargoBike, 'cb', cargoBike, userId);
await entityManager.getRepository(CargoBike) await entityManager.getRepository(CargoBike)
@ -134,7 +133,7 @@ export class CargoBikeAPI extends DataSource {
async deleteCargoBike (id: number, userId: number) { async deleteCargoBike (id: number, userId: number) {
return await this.connection.transaction(async (entityManager: EntityManager) => { return await this.connection.transaction(async (entityManager: EntityManager) => {
if (await LockUtils.isLocked(entityManager, CargoBike, 'cb', id, userId)) { if (await LockUtils.isLocked(entityManager, CargoBike, 'cb', id, userId)) {
throw new UserInputError('Attempting to soft delete locked resource'); throw new ResourceLockedError('CargoBike', 'Attempting to soft delete locked resource');
} }
await ActionLogger.log(entityManager, CargoBike, 'bg', { id: id }, userId, Actions.SOFT_DELETE); await ActionLogger.log(entityManager, CargoBike, 'bg', { id: id }, userId, Actions.SOFT_DELETE);
return await entityManager.getRepository(CargoBike) return await entityManager.getRepository(CargoBike)
@ -189,7 +188,7 @@ export class CargoBikeAPI extends DataSource {
delete bikeEvent.keepLock; delete bikeEvent.keepLock;
await this.connection.transaction(async (entityManager: EntityManager) => { await this.connection.transaction(async (entityManager: EntityManager) => {
if (await LockUtils.isLocked(entityManager, BikeEvent, 'be', bikeEvent.id, userId)) { if (await LockUtils.isLocked(entityManager, BikeEvent, 'be', bikeEvent.id, userId)) {
throw new GraphQLError('BikeEvent locked by other user'); throw new ResourceLockedError('BikeEvents');
} }
await ActionLogger.log(entityManager, BikeEvent, 'be', bikeEvent, userId); await ActionLogger.log(entityManager, BikeEvent, 'be', bikeEvent, userId);
await entityManager.getRepository(BikeEvent) await entityManager.getRepository(BikeEvent)
@ -267,7 +266,7 @@ export class CargoBikeAPI extends DataSource {
delete bikeEventType.keepLock; delete bikeEventType.keepLock;
await this.connection.transaction(async (entityManager: EntityManager) => { await this.connection.transaction(async (entityManager: EntityManager) => {
if (await LockUtils.isLocked(entityManager, BikeEventType, 'bet', bikeEventType.id, userId)) { if (await LockUtils.isLocked(entityManager, BikeEventType, 'bet', bikeEventType.id, userId)) {
throw new GraphQLError('BikeEventType locked by other user'); throw new ResourceLockedError('BikeEventType');
} }
await ActionLogger.log(entityManager, BikeEventType, 'bet', bikeEventType, userId); await ActionLogger.log(entityManager, BikeEventType, 'bet', bikeEventType, userId);
await entityManager.getRepository(BikeEventType) await entityManager.getRepository(BikeEventType)
@ -405,7 +404,7 @@ export class CargoBikeAPI extends DataSource {
delete equipment.keepLock; delete equipment.keepLock;
await this.connection.transaction(async (entityManager: EntityManager) => { await this.connection.transaction(async (entityManager: EntityManager) => {
if (await LockUtils.isLocked(entityManager, Equipment, 'equipment', equipment.id, userId)) { if (await LockUtils.isLocked(entityManager, Equipment, 'equipment', equipment.id, userId)) {
return new GraphQLError('Equipment is locked by other user'); throw new ResourceLockedError('Equipment');
} }
await ActionLogger.log(entityManager, Equipment, 'e', equipment, userId); await ActionLogger.log(entityManager, Equipment, 'e', equipment, userId);
await entityManager.getRepository(Equipment) await entityManager.getRepository(Equipment)
@ -453,7 +452,7 @@ export class CargoBikeAPI extends DataSource {
delete equipmentType.keepLock; delete equipmentType.keepLock;
await this.connection.transaction(async (entityManager: EntityManager) => { await this.connection.transaction(async (entityManager: EntityManager) => {
if (await LockUtils.isLocked(entityManager, EquipmentType, 'et', equipmentType.id, userId)) { if (await LockUtils.isLocked(entityManager, EquipmentType, 'et', equipmentType.id, userId)) {
throw new GraphQLError('EquipmentType is locked by other user'); throw new ResourceLockedError('EquipmentType');
} }
await ActionLogger.log(entityManager, EquipmentType, 'et', equipmentType, userId); await ActionLogger.log(entityManager, EquipmentType, 'et', equipmentType, userId);
await entityManager.getRepository(EquipmentType) await entityManager.getRepository(EquipmentType)

@ -24,6 +24,7 @@ import { Person } from '../../model/Person';
import { ActionLogger, DBUtils, LockUtils } from './utils'; import { ActionLogger, DBUtils, LockUtils } from './utils';
import { GraphQLError } from 'graphql'; import { GraphQLError } from 'graphql';
import { LendingStation } from '../../model/LendingStation'; import { LendingStation } from '../../model/LendingStation';
import { ResourceLockedError } from '../../errors/ResourceLockedError';
export class ContactInformationAPI extends DataSource { export class ContactInformationAPI extends DataSource {
connection : Connection connection : Connection
@ -68,7 +69,7 @@ export class ContactInformationAPI extends DataSource {
delete person.keepLock; delete person.keepLock;
await this.connection.transaction(async (entityManger: EntityManager) => { await this.connection.transaction(async (entityManger: EntityManager) => {
if (await LockUtils.isLocked(entityManger, Person, 'p', person.id, userId)) { if (await LockUtils.isLocked(entityManger, Person, 'p', person.id, userId)) {
throw new GraphQLError('Person is locker by another user'); throw new ResourceLockedError('Person');
} }
await ActionLogger.log(entityManger, Person, 'p', person, userId); await ActionLogger.log(entityManger, Person, 'p', person, userId);
await entityManger.getRepository(Person) await entityManger.getRepository(Person)
@ -146,7 +147,7 @@ export class ContactInformationAPI extends DataSource {
delete contactInformation.keepLock; delete contactInformation.keepLock;
await this.connection.transaction(async (entityManager: EntityManager) => { await this.connection.transaction(async (entityManager: EntityManager) => {
if (await LockUtils.isLocked(entityManager, ContactInformation, 'ci', contactInformation.id, userId)) { if (await LockUtils.isLocked(entityManager, ContactInformation, 'ci', contactInformation.id, userId)) {
throw new GraphQLError('ContactInformation is locked by other user'); throw new ResourceLockedError('ContactInformation');
} }
await ActionLogger.log(entityManager, ContactInformation, 'ci', contactInformation, userId); await ActionLogger.log(entityManager, ContactInformation, 'ci', contactInformation, userId);
await entityManager.getRepository(ContactInformation) await entityManager.getRepository(ContactInformation)

@ -24,6 +24,7 @@ import { CargoBike } from '../../model/CargoBike';
import { LendingStation } from '../../model/LendingStation'; import { LendingStation } from '../../model/LendingStation';
import { TimeFrame } from '../../model/TimeFrame'; import { TimeFrame } from '../../model/TimeFrame';
import { ActionLogger, genDateRange, DBUtils, LockUtils } from './utils'; import { ActionLogger, genDateRange, DBUtils, LockUtils } from './utils';
import { ResourceLockedError } from '../../errors/ResourceLockedError';
export class LendingStationAPI extends DataSource { export class LendingStationAPI extends DataSource {
connection : Connection connection : Connection
@ -163,7 +164,7 @@ export class LendingStationAPI extends DataSource {
delete lendingStation.keepLock; delete lendingStation.keepLock;
await this.connection.transaction(async (entityManager: EntityManager) => { await this.connection.transaction(async (entityManager: EntityManager) => {
if (await LockUtils.isLocked(entityManager, LendingStation, 'ls', lendingStation.id, userId)) { if (await LockUtils.isLocked(entityManager, LendingStation, 'ls', lendingStation.id, userId)) {
throw new UserInputError('Attempting to update locked resource'); throw new ResourceLockedError('LendingStation', 'Attempting to update locked resource');
} }
await ActionLogger.log(entityManager, LendingStation, 'ls', lendingStation, userId); await ActionLogger.log(entityManager, LendingStation, 'ls', lendingStation, userId);
await entityManager.getRepository(LendingStation) await entityManager.getRepository(LendingStation)
@ -222,7 +223,7 @@ export class LendingStationAPI extends DataSource {
delete timeFrame.keepLock; delete timeFrame.keepLock;
await this.connection.transaction(async (entityManager: EntityManager) => { await this.connection.transaction(async (entityManager: EntityManager) => {
if (await LockUtils.isLocked(entityManager, TimeFrame, 'tf', timeFrame.id, userId)) { if (await LockUtils.isLocked(entityManager, TimeFrame, 'tf', timeFrame.id, userId)) {
throw new UserInputError('Attempting to update locked resource'); throw new ResourceLockedError('TimeFrame', 'Attempting to update locked resource');
} }
genDateRange(timeFrame); genDateRange(timeFrame);
await ActionLogger.log(entityManager, TimeFrame, 'tf', timeFrame, userId); await ActionLogger.log(entityManager, TimeFrame, 'tf', timeFrame, userId);

@ -26,6 +26,7 @@ import { EngagementType } from '../../model/EngagementType';
import { ActionLogger, DBUtils, genDateRange, LockUtils } from './utils'; import { ActionLogger, DBUtils, genDateRange, LockUtils } from './utils';
import { UserInputError } from 'apollo-server-express'; import { UserInputError } from 'apollo-server-express';
import { GraphQLError } from 'graphql'; import { GraphQLError } from 'graphql';
import { ResourceLockedError } from '../../errors/ResourceLockedError';
export class ParticipantAPI extends DataSource { export class ParticipantAPI extends DataSource {
connection : Connection connection : Connection
@ -202,7 +203,7 @@ export class ParticipantAPI extends DataSource {
delete participant.keepLock; delete participant.keepLock;
await this.connection.transaction(async (entityManager: EntityManager) => { await this.connection.transaction(async (entityManager: EntityManager) => {
if (await LockUtils.isLocked(entityManager, Participant, 'p', participant.id, userId)) { if (await LockUtils.isLocked(entityManager, Participant, 'p', participant.id, userId)) {
throw new UserInputError('Attempting to update locked resource'); throw new ResourceLockedError('Participant', 'Attempting to update locked resource');
} }
genDateRange(participant); genDateRange(participant);
const workshops = participant.workshopIds; const workshops = participant.workshopIds;
@ -288,7 +289,7 @@ export class ParticipantAPI extends DataSource {
genDateRange(engagement); genDateRange(engagement);
await this.connection.transaction(async (entityManager: EntityManager) => { await this.connection.transaction(async (entityManager: EntityManager) => {
if (await LockUtils.isLocked(entityManager, Engagement, 'e', engagement.id, userId)) { if (await LockUtils.isLocked(entityManager, Engagement, 'e', engagement.id, userId)) {
throw new GraphQLError('Engagement is locked by other user'); throw new ResourceLockedError('Engagement');
} }
await ActionLogger.log(entityManager, Engagement, 'e', engagement, userId); await ActionLogger.log(entityManager, Engagement, 'e', engagement, userId);
// check for overlapping engagements // check for overlapping engagements
@ -356,7 +357,7 @@ export class ParticipantAPI extends DataSource {
delete engagementType.keepLock; delete engagementType.keepLock;
await this.connection.transaction(async (entityManager: EntityManager) => { await this.connection.transaction(async (entityManager: EntityManager) => {
if (await LockUtils.isLocked(entityManager, EngagementType, 'et', engagementType.id, userId)) { if (await LockUtils.isLocked(entityManager, EngagementType, 'et', engagementType.id, userId)) {
throw new GraphQLError('EngagementType is locked by other user'); throw new ResourceLockedError('EngagementType');
} }
await ActionLogger.log(entityManager, EngagementType, 'et', engagementType, userId); await ActionLogger.log(entityManager, EngagementType, 'et', engagementType, userId);
await entityManager.getRepository(EngagementType) await entityManager.getRepository(EngagementType)

@ -26,6 +26,7 @@ import { CargoBike } from '../../model/CargoBike';
import { LendingStation } from '../../model/LendingStation'; import { LendingStation } from '../../model/LendingStation';
import { ActionLogger, DBUtils, LockUtils } from './utils'; import { ActionLogger, DBUtils, LockUtils } from './utils';
import { GraphQLError } from 'graphql'; import { GraphQLError } from 'graphql';
import { ResourceLockedError } from '../../errors/ResourceLockedError';
export class ProviderAPI extends DataSource { export class ProviderAPI extends DataSource {
connection : Connection connection : Connection
@ -150,7 +151,7 @@ export class ProviderAPI extends DataSource {
delete provider.cargoBikeIds; delete provider.cargoBikeIds;
await this.connection.transaction(async (entityManager: EntityManager) => { await this.connection.transaction(async (entityManager: EntityManager) => {
if (await LockUtils.isLocked(entityManager, Provider, 'p', provider.id, userId)) { if (await LockUtils.isLocked(entityManager, Provider, 'p', provider.id, userId)) {
throw new GraphQLError('Provider is locked by another user'); throw new ResourceLockedError('Provider');
} }
await ActionLogger.log(entityManager, Provider, 'p', provider, userId); await ActionLogger.log(entityManager, Provider, 'p', provider, userId);
await entityManager.getRepository(Provider) await entityManager.getRepository(Provider)
@ -198,7 +199,7 @@ export class ProviderAPI extends DataSource {
delete organisation.keepLock; delete organisation.keepLock;
await this.connection.transaction(async (entityManager: EntityManager) => { await this.connection.transaction(async (entityManager: EntityManager) => {
if (await LockUtils.isLocked(entityManager, Organisation, 'o', organisation.id, userId)) { if (await LockUtils.isLocked(entityManager, Organisation, 'o', organisation.id, userId)) {
throw new GraphQLError('Organisation is locked by another user'); throw new ResourceLockedError('Organisation');
} }
await ActionLogger.log(entityManager, Organisation, 'o', organisation, userId); await ActionLogger.log(entityManager, Organisation, 'o', organisation, userId);
await entityManager.getRepository(Organisation) await entityManager.getRepository(Organisation)

@ -21,6 +21,7 @@ import { Connection, EntityManager, ObjectType } from 'typeorm';
import { Lockable } from '../../model/CargoBike'; import { Lockable } from '../../model/CargoBike';
import { ActionLog, Actions } from '../../model/ActionLog'; import { ActionLog, Actions } from '../../model/ActionLog';
import { UserInputError } from 'apollo-server-express'; import { UserInputError } from 'apollo-server-express';
import { ResourceLockedError } from '../../errors/ResourceLockedError';
export function genDateRange (struct: any) { export function genDateRange (struct: any) {
if (!struct.dateRange || !struct.dateRange.from) { if (!struct.dateRange || !struct.dateRange.from) {
@ -100,7 +101,7 @@ export class DBUtils {
static async deleteEntity (connection: Connection, target: ObjectType<Lockable>, alias: string, id: number, userId: number): Promise<Boolean> { static async deleteEntity (connection: Connection, target: ObjectType<Lockable>, alias: string, id: number, userId: number): Promise<Boolean> {
return await connection.transaction(async (entityManger: EntityManager) => { return await connection.transaction(async (entityManger: EntityManager) => {
if (await LockUtils.isLocked(entityManger, target, alias, id, userId)) { if (await LockUtils.isLocked(entityManger, target, alias, id, userId)) {
throw new UserInputError('Attempting to delete locked resource'); throw new ResourceLockedError('Connection', 'Attempting to delete locked resource');
} }
await ActionLogger.log(entityManger, target, alias, { id: id }, userId, Actions.DELETE); await ActionLogger.log(entityManger, target, alias, { id: id }, userId, Actions.DELETE);
return await entityManger.getRepository(target) return await entityManger.getRepository(target)

@ -25,6 +25,7 @@ import { ActionLogger, DBUtils, LockUtils } from './utils';
import { UserInputError } from 'apollo-server-express'; import { UserInputError } from 'apollo-server-express';
import { GraphQLError } from 'graphql'; import { GraphQLError } from 'graphql';
import { Participant } from '../../model/Participant'; import { Participant } from '../../model/Participant';
import { ResourceLockedError } from '../../errors/ResourceLockedError';
export class WorkshopAPI extends DataSource { export class WorkshopAPI extends DataSource {
connection: Connection connection: Connection
@ -57,7 +58,7 @@ export class WorkshopAPI extends DataSource {
delete workshop.keepLock; delete workshop.keepLock;
await this.connection.transaction(async (entityManger: EntityManager) => { await this.connection.transaction(async (entityManger: EntityManager) => {
if (await LockUtils.isLocked(entityManger, Workshop, 'w', workshop.id, userId)) { if (await LockUtils.isLocked(entityManger, Workshop, 'w', workshop.id, userId)) {
throw new UserInputError('Attempting to update locked resource'); throw new ResourceLockedError('Workshop', 'Attempting to update locked resource');
} }
await ActionLogger.log(entityManger, Workshop, 'w', workshop, userId); await ActionLogger.log(entityManger, Workshop, 'w', workshop, userId);
await entityManger.getRepository(Workshop) await entityManger.getRepository(Workshop)
@ -99,7 +100,7 @@ export class WorkshopAPI extends DataSource {
delete workshopType.keepLock; delete workshopType.keepLock;
await this.connection.transaction(async (entityManager: EntityManager) => { await this.connection.transaction(async (entityManager: EntityManager) => {
if (await LockUtils.isLocked(entityManager, WorkshopType, 'wt', workshopType.id, userId)) { if (await LockUtils.isLocked(entityManager, WorkshopType, 'wt', workshopType.id, userId)) {
throw new UserInputError('Attempting to update locked resource'); throw new ResourceLockedError('WorkshopType', 'Attempting to update locked resource');
} }
await ActionLogger.log(entityManager, WorkshopType, 'wt', workshopType, userId); await ActionLogger.log(entityManager, WorkshopType, 'wt', workshopType, userId);
await entityManager.getRepository(WorkshopType) await entityManager.getRepository(WorkshopType)

@ -0,0 +1,11 @@
import { ApolloError } from 'apollo-server-express';
export class ResourceLockedError extends ApolloError {
constructor (resourceName: String, additionalContext?: string) {
if (additionalContext) {
super(`The Resource ${resourceName} is currently locked: ${additionalContext}`, 'LOCKED_ERROR');
} else {
super(`The Resource ${resourceName} is currently locked`, 'LOCKED_ERROR');
}
}
}

@ -20,9 +20,15 @@ export const CREATE_CARGO_BIKE = gql`
dimensionsAndLoad: { dimensionsAndLoad: {
hasCoverBox: true hasCoverBox: true
lockable:false lockable:false
boxLength: 0.1 boxLengthRange: {
boxWidth: 0.2 min: 0,
boxHeight:0.3 }
boxWidthRange: {
min: 0.2
}
boxHeightRange: {
min: 0.3
}
maxWeightBox: 1.1 maxWeightBox: 1.1
maxWeightLuggageRack: 1.2 maxWeightLuggageRack: 1.2
maxWeightTotal: 1.3 maxWeightTotal: 1.3

Loading…
Cancel
Save