Skip to content

Commit

Permalink
Request log (#47)
Browse files Browse the repository at this point in the history
* Remove Event (originally added for audits, unused)
* core implementation.
  • Loading branch information
pwalsh committed Feb 15, 2022
1 parent 6d3e547 commit 48acfb8
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 243 deletions.
2 changes: 0 additions & 2 deletions src/core/events/index.ts

This file was deleted.

49 changes: 0 additions & 49 deletions src/core/events/models.ts

This file was deleted.

40 changes: 0 additions & 40 deletions src/core/events/repositories.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { internalServerError, notFound } from '../middlewares';
import type { ModelDefinition, PluginBase } from '../types';
import { CommunicationModel, CommunicationRepository } from './communications';
import { DepartmentModel, DepartmentRepository, departmentRouter } from './departments';
import { EventModel, EventRepository } from './events';
import { JurisdictionModel, JurisdictionRepository, jurisdictionRouter } from './jurisdictions';
import { open311Router } from './open311';
import { ServiceRequestCommentModel, ServiceRequestModel, ServiceRequestRepository, serviceRequestRouter } from './service-requests';
Expand Down Expand Up @@ -34,7 +33,6 @@ const coreModels: ModelDefinition[] = [
ServiceRequestCommentModel,
ServiceModel,
StaffUserModel,
EventModel,
CommunicationModel,
DepartmentModel,
]
Expand All @@ -44,7 +42,6 @@ const coreRepositories: PluginBase[] = [
ServiceRequestRepository,
ServiceRepository,
StaffUserRepository,
EventRepository,
CommunicationRepository,
DepartmentRepository
]
Expand Down
28 changes: 0 additions & 28 deletions src/core/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,6 @@ export function isValidToSchema(schema: JSONSchema): (value: JSONBTarget) => Val
}
}

export const eventActorSchema: JSONSchema = {
'$schema': 'https://json-schema.org/draft/2020-12/schema',
'$id': `${homepage}/src/core/schemas/event-actor-schema.json`,
'title': 'Event Actor Schema',
'description': 'Event actors initiate events, and are users or the system itself.',
'type': 'object',
'required': ['id', 'name', 'type'],
'properties': {
'id': { 'type': 'string' },
'name': { 'type': 'string' },
'type': { 'type': 'string' },
}
}

export const eventSenderSchema: JSONSchema = {
'$schema': 'https://json-schema.org/draft/2020-12/schema',
'$id': `${homepage}/src/core/schemas/event-sender-schema.json`,
'title': 'Event Sender Schema',
'description': 'Event senders are subsystems that events are triggered from.',
'type': 'object',
'required': ['id', 'name', 'type'],
'properties': {
'id': { 'type': 'string' },
'module': { 'type': 'string' },
'callable': { 'type': 'string' },
}
}

export const serviceExtraAttrsSchema: JSONSchema = {
'$schema': 'https://json-schema.org/draft/2020-12/schema',
'$id': `${homepage}/src/core/schemas/service-attr-schema.json`,
Expand Down
9 changes: 9 additions & 0 deletions src/core/service-requests/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { StaffUserAttributes } from "../../types";

export function makeAuditMessage(user: StaffUserAttributes | undefined, fieldName: string, oldValue:string, newValue:string): string {
let displayName = 'System';
if (user) {
displayName = user.displayName;
}
return `${displayName} changed this request ${fieldName} from ${oldValue} to ${newValue}`;
}
33 changes: 26 additions & 7 deletions src/core/service-requests/repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import _ from 'lodash';
import sequelize from 'sequelize';
import { queryParamsToSequelize } from '../../helpers';
import { appIds } from '../../registry/service-identifiers';
import type { AppSettings, IServiceRequestRepository, Models, QueryParamsAll, ServiceRequestAttributes, ServiceRequestCommentAttributes, ServiceRequestCommentInstance, ServiceRequestInstance, ServiceRequestStatusAttributes } from '../../types';
import type { AppSettings, IServiceRequestRepository, Models, QueryParamsAll, ServiceRequestAttributes, ServiceRequestCommentAttributes, ServiceRequestCommentCreateAttributes, ServiceRequestCommentInstance, ServiceRequestInstance, ServiceRequestStatusAttributes, StaffUserAttributes } from '../../types';
import { makeAuditMessage } from './helpers';
import { REQUEST_STATUSES } from './models';


@injectable()
export class ServiceRequestRepository implements IServiceRequestRepository {

Expand Down Expand Up @@ -93,11 +95,12 @@ export class ServiceRequestRepository implements IServiceRequestRepository {
async createComment(
jurisdictionId: string,
serviceRequestId: string,
data: ServiceRequestCommentAttributes
data: ServiceRequestCommentCreateAttributes,
user?: StaffUserAttributes
): Promise<ServiceRequestCommentAttributes> {
const { ServiceRequestComment } = this.models;
const record = await ServiceRequestComment.create(
Object.assign({}, data, { serviceRequestId })
Object.assign({}, data, { serviceRequestId, addedBy: user?.id })
) as ServiceRequestCommentInstance;
return record;
}
Expand All @@ -119,35 +122,51 @@ export class ServiceRequestRepository implements IServiceRequestRepository {
return await record.save();
}

async updateStatus(jurisdictionId: string, id: string, status: string): Promise<ServiceRequestAttributes> {
async updateStatus(
jurisdictionId: string, id: string, status: string, user?: StaffUserAttributes
): Promise<ServiceRequestAttributes> {
const { ServiceRequest } = this.models;
let record = await ServiceRequest.findByPk(id) as ServiceRequestInstance;
const auditMessage = makeAuditMessage(user, '"status"', record.status, status);
record.status = status;
record = await record.save();
await this.createComment(jurisdictionId, record.id, {comment: auditMessage, addedBy: user?.id});
return record;
}

async updateAssignedTo(jurisdictionId: string, id: string, assignedTo: string): Promise<ServiceRequestAttributes> {
async updateAssignedTo(
jurisdictionId: string, id: string, assignedTo: string, user?: StaffUserAttributes
): Promise<ServiceRequestAttributes> {
const { ServiceRequest } = this.models;
let record = await ServiceRequest.findByPk(id) as ServiceRequestInstance;
const auditMessage = makeAuditMessage(user, '"assigned to"', record.assignedTo, assignedTo);
record.assignedTo = assignedTo;
record = await record.save();
await this.createComment(jurisdictionId, record.id, {comment: auditMessage});
return record;
}

async updateDepartment(jurisdictionId: string, id: string, department: string): Promise<ServiceRequestAttributes> {
async updateDepartment(
jurisdictionId: string, id: string, department: string, user?: StaffUserAttributes
): Promise<ServiceRequestAttributes> {
const { ServiceRequest } = this.models;
let record = await ServiceRequest.findByPk(id) as ServiceRequestInstance;
const auditMessage = makeAuditMessage(user, '"department"', record.departmentId, department);
record.departmentId = department;
record = await record.save();
await this.createComment(jurisdictionId, record.id, {comment: auditMessage});
return record;
}

async updateService(jurisdictionId: string, id: string, service: string): Promise<ServiceRequestAttributes> {
async updateService(
jurisdictionId: string, id: string, service: string, user?: StaffUserAttributes
): Promise<ServiceRequestAttributes> {
const { ServiceRequest } = this.models;
let record = await ServiceRequest.findByPk(id) as ServiceRequestInstance;
const auditMessage = makeAuditMessage(user, '"service"', record.serviceId as string, service);
record.serviceId = service;
record = await record.save();
await this.createComment(jurisdictionId, record.id, {comment: auditMessage});
return record;
}
}
8 changes: 5 additions & 3 deletions src/core/service-requests/routes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Request, Response, Router } from 'express';
import { serviceRequestFiltersToSequelize, wrapHandler } from '../../helpers';
import { resolveJurisdiction, enforceJurisdictionAccess } from '../../middlewares';
import { ServiceRequestAttributes } from '../../types';
import { enforceJurisdictionAccess, resolveJurisdiction } from '../../middlewares';
import { ServiceRequestAttributes, StaffUserAttributes } from '../../types';
import { GovFlowEmitter } from '../event-listeners';
import { SERVICE_REQUEST_CLOSED_STATES } from '../service-requests';

Expand Down Expand Up @@ -65,7 +65,9 @@ serviceRequestRouter.post('/service', wrapHandler(async (req: Request, res: Resp
serviceRequestRouter.post('/comments/:serviceRequestId', wrapHandler(async (req: Request, res: Response) => {
const { ServiceRequest } = res.app.repositories;
const { serviceRequestId } = req.params;
const record = await ServiceRequest.createComment(req.jurisdiction.id, serviceRequestId, req.body);
const record = await ServiceRequest.createComment(
req.jurisdiction.id, serviceRequestId, req.body, req.user as StaffUserAttributes | undefined
);
res.status(200).send({ data: record });
}))

Expand Down
4 changes: 0 additions & 4 deletions src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ function applyCoreModelRelations(models: Models) {
Jurisdiction,
StaffUser,
ServiceRequestComment,
Event,
Communication,
Department
} = models;
Expand All @@ -39,9 +38,6 @@ function applyCoreModelRelations(models: Models) {
Jurisdiction.hasMany(ServiceRequest, { as: 'requests', foreignKey: 'jurisdictionId' });
ServiceRequest.belongsTo(Jurisdiction, { as: 'jurisdiction' });

Jurisdiction.hasMany(Event, { as: 'events', foreignKey: 'jurisdictionId' });
Event.belongsTo(Jurisdiction, { as: 'jurisdiction' });

Service.hasMany(ServiceRequest, { as: 'requests', foreignKey: 'serviceId' });
ServiceRequest.belongsTo(Service, { as: 'service' });

Expand Down
47 changes: 47 additions & 0 deletions src/migrations/10_remove_events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import type { QueryInterface } from 'sequelize';
import { DataTypes } from 'sequelize';

export async function up({ context: queryInterface }: Record<string, QueryInterface>): Promise<void> {
await queryInterface.dropTable('Event');
}

export async function down({ context: queryInterface }: Record<string, QueryInterface>): Promise<void> {
await queryInterface.createTable('Event', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
primaryKey: true,
},
sender: {
type: DataTypes.JSONB,
allowNull: false,
},
actor: {
type: DataTypes.JSONB,
allowNull: false,
},
message: {
type: DataTypes.STRING,
allowNull: false,
},
createdAt: {
allowNull: false,
type: DataTypes.DATE
},
updatedAt: {
allowNull: false,
type: DataTypes.DATE
},
jurisdictionId: {
type: DataTypes.STRING,
onDelete: 'CASCADE',
references: {
model: 'Jurisdiction',
key: 'id',
}
},
});
await queryInterface.addIndex('Event', ['createdAt']);
await queryInterface.addIndex('Event', ['updatedAt']);
}
6 changes: 1 addition & 5 deletions src/registry/repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { Container } from 'inversify';
import { CommunicationRepository } from '../core/communications';
import { CommunicationService } from '../core/communications/services';
import { DepartmentRepository } from '../core/departments';
import { EventRepository } from '../core/events';
import { JurisdictionRepository } from '../core/jurisdictions';
import { Open311ServiceRepository, Open311ServiceRequestRepository } from '../core/open311';
import { ServiceRequestRepository } from '../core/service-requests';
import { ServiceRepository } from '../core/services';
import { StaffUserRepository } from '../core/staff-users';
import type { AppSettings, ICommunicationRepository, ICommunicationService, IDepartmentRepository, IEventRepository, IJurisdictionRepository, IOpen311ServiceRepository, IOpen311ServiceRequestRepository, IServiceRepository, IServiceRequestRepository, IStaffUserRepository, Plugin, Services } from '../types';
import type { AppSettings, ICommunicationRepository, ICommunicationService, IDepartmentRepository, IJurisdictionRepository, IOpen311ServiceRepository, IOpen311ServiceRequestRepository, IServiceRepository, IServiceRequestRepository, IStaffUserRepository, Plugin, Services } from '../types';
import { DatabaseEngine, Models, Repositories } from '../types';
import { appIds, repositoryIds, serviceIds } from './service-identifiers';

Expand Down Expand Up @@ -37,7 +36,6 @@ function bindRepositoriesWithPlugins(
repositoryContainer.bind<IOpen311ServiceRequestRepository>(repositoryIds.IOpen311ServiceRequestRepository).to(
Open311ServiceRequestRepository
);
repositoryContainer.bind<IEventRepository>(repositoryIds.IEventRepository).to(EventRepository);
repositoryContainer.bind<ICommunicationRepository>(repositoryIds.ICommunicationRepository).to(
CommunicationRepository
);
Expand All @@ -63,7 +61,6 @@ function bindRepositoriesWithPlugins(
const Open311ServiceRequest = repositoryContainer.get<IOpen311ServiceRequestRepository>(
repositoryIds.IOpen311ServiceRequestRepository
);
const Event = repositoryContainer.get<IEventRepository>(repositoryIds.IEventRepository);
const Communication = repositoryContainer.get<ICommunicationRepository>(repositoryIds.ICommunicationRepository);
const Department = repositoryContainer.get<IDepartmentRepository>(repositoryIds.IDepartmentRepository);

Expand All @@ -74,7 +71,6 @@ function bindRepositoriesWithPlugins(
ServiceRequest,
Open311Service,
Open311ServiceRequest,
Event,
Communication,
Department
}
Expand Down
Loading

0 comments on commit 48acfb8

Please sign in to comment.