Skip to content

Commit

Permalink
add prefix variable to doc type
Browse files Browse the repository at this point in the history
  • Loading branch information
mgtennant committed Jul 20, 2024
1 parent b88520d commit ae3eb93
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 41 deletions.
13 changes: 11 additions & 2 deletions backend/src/admin/admin.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,17 @@ export class AdminController {
}

@Post('add-document-type')
addDocumentType(@User() user: IdirObject, @Body() data: { name: string; created_by: string; created_date: string }) {
return this.adminService.addDocumentType(data.name, data.created_by, data.created_date, user.idir_username);
addDocumentType(
@User() user: IdirObject,
@Body() data: { name: string; prefix: string; created_by: string; created_date: string }
) {
return this.adminService.addDocumentType(
data.name,
data.prefix,
data.created_by,
data.created_date,
user.idir_username
);
}

@Get('remove-document-type/:id')
Expand Down
3 changes: 2 additions & 1 deletion backend/src/admin/admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,10 @@ export class AdminService {
return this.provisionService.disable(id);
}

async addDocumentType(name: string, created_by: string, created_date: string, create_userid: string) {
async addDocumentType(name: string, prefix: string, created_by: string, created_date: string, create_userid: string) {
const documentType: DocumentType = await this.documentTypeService.add(
name,
prefix,
created_by,
created_date,
create_userid
Expand Down
11 changes: 9 additions & 2 deletions backend/src/document_type/document_type.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ export class DocumentTypeController {

@Post('update')
update(
@Body() data: { id: number; name: string; created_by: string; created_date: string },
@Body() data: { id: number; name: string; prefix: string; created_by: string; created_date: string },
@User() user: IdirObject
) {
return this.documentTypeService.update(data.id, data.name, data.created_by, data.created_date, user.idir_username);
return this.documentTypeService.update(
data.id,
data.name,
data.prefix,
data.created_by,
data.created_date,
user.idir_username
);
}

@Get()
Expand Down
6 changes: 4 additions & 2 deletions backend/src/document_type/document_type.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ export class DocumentTypeService {
return this.documentTypeRepository.find();
}

add(name: string, created_by: string, created_date: string, userid: string): Promise<DocumentType> {
add(name: string, prefix: string, created_by: string, created_date: string, userid: string): Promise<DocumentType> {
const newDocumentType = this.documentTypeRepository.create({
name: name,
prefix: prefix,
created_by: created_by,
created_date: created_date,
create_userid: userid,
Expand All @@ -47,11 +48,12 @@ export class DocumentTypeService {
async update(
id: number,
name: string,
prefix: string,
created_by: string,
created_date: string,
userid: string
): Promise<DocumentType> {
await this.documentTypeRepository.update(id, { name, created_by, created_date, update_userid: userid });
await this.documentTypeRepository.update(id, { name, prefix, created_by, created_date, update_userid: userid });
const updatedDocumentType = await this.documentTypeRepository.findOneBy({ id });
if (!updatedDocumentType) {
throw new Error('DocumentType not found');
Expand Down
3 changes: 3 additions & 0 deletions backend/src/document_type/entities/document_type.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export class DocumentType {
@Column()
name: string;

@Column()
prefix: string;

@Column({ nullable: true })
created_by: string;

Expand Down
38 changes: 21 additions & 17 deletions backend/src/report/report.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,28 @@ export class ReportService {
async generateReportName(dtid: number, tenure_file_number: string, document_type_id: number) {
const version = await this.documentDataLogService.findNextVersion(dtid, document_type_id);
const documentType = await this.documentTypeService.findById(document_type_id);
// probably a better way of doing this
let prefix = 'report';
let prefix = 'report'; // fallback prefix
if (documentType) {
if (documentType.name.toLowerCase().includes('notice of final review')) {
prefix = 'NFR';
} else if (documentType.name.toLowerCase().includes('land use report')) {
prefix = 'LUR';
} else if (
documentType.name.toLowerCase().includes('grazing') ||
documentType.name.toLowerCase().includes('lease')
) {
prefix = 'Lease';
} else if (documentType.name.toLowerCase().includes('standard licence')) {
prefix = 'Licence';
} else if (documentType.name.toLowerCase().includes('assumption')) {
prefix = 'Assignment';
} else if (documentType.name.toLowerCase().includes('modification')) {
prefix = 'Modification';
prefix = documentType.prefix;
console.log('prefix pulled from db::: ' + prefix);
// fallback logic left in for now
if (prefix === '' || prefix === null || prefix === 'report') {
if (documentType.name.toLowerCase().includes('notice of final review')) {
prefix = 'NFR';
} else if (documentType.name.toLowerCase().includes('land use report')) {
prefix = 'LUR';
} else if (
documentType.name.toLowerCase().includes('grazing') ||
documentType.name.toLowerCase().includes('lease')
) {
prefix = 'Lease';
} else if (documentType.name.toLowerCase().includes('standard licence')) {
prefix = 'Licence';
} else if (documentType.name.toLowerCase().includes('assumption')) {
prefix = 'Assignment';
} else if (documentType.name.toLowerCase().includes('modification')) {
prefix = 'Modification';
}
}
}
return {
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/app/common/manage-doc-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,24 @@ export const getDocumentTypes = () => {
return api.get<DocType[]>(getParameters);
};

export const addDocType = (name: string, created_by: string, created_date: string) => {
export const addDocType = (name: string, prefix: string, created_by: string, created_date: string) => {
const url = `${config.API_BASE_URL}/admin/add-document-type`;
const data = {
name,
prefix,
created_by,
created_date,
};
const postParameters = api.generateApiParameters(url, data);
return api.post<DocType>(postParameters);
};

export const updateDocType = (id: number, name: string, created_by: string, created_date: string) => {
export const updateDocType = (id: number, name: string, prefix: string, created_by: string, created_date: string) => {
const url = `${config.API_BASE_URL}/document-type/update`;
const data = {
id,
name,
prefix,
created_by,
created_date,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { FC, useEffect, useState } from 'react';
import { Button, Col, Form, Modal, Row, Spinner } from 'react-bootstrap';
import { FC, useState } from 'react';
import { Button, Col, Form, Modal, OverlayTrigger, Row, Spinner, Tooltip } from 'react-bootstrap';
import { DocType } from '../../../types/types';
import UserService from '../../../service/user-service';
import '@fortawesome/fontawesome-free/css/all.min.css';

interface AddDocTypeModalProps {
allDocTypes: DocType[];
show: boolean;
onHide: () => void;
onAdd: (name: string, created_by: string, created_date: string) => void;
onAdd: (name: string, prefix: string, created_by: string, created_date: string) => void;
}

const AddDocTypeModal: FC<AddDocTypeModalProps> = ({ allDocTypes, show, onHide, onAdd }) => {
const username = UserService.getUsername();
const [name, setName] = useState<string>('');
const [prefix, setPrefix] = useState<string>('');
const [createdBy, setCreatedBy] = useState<string>(username);
const [createdDate, setCreatedDate] = useState<string>(new Date().toISOString().substring(0, 10));
const [loading, setLoading] = useState<boolean>(false);
Expand All @@ -26,7 +28,7 @@ const AddDocTypeModal: FC<AddDocTypeModalProps> = ({ allDocTypes, show, onHide,
setShowError(false);
const isNameUnique = !allDocTypes.some((docType) => docType.name === name);
if (isNameUnique) {
onAdd(name, createdBy, createdDate);
onAdd(name, prefix, createdBy, createdDate);
onHide();
} else {
setError('That document type name already exists');
Expand All @@ -46,6 +48,11 @@ const AddDocTypeModal: FC<AddDocTypeModalProps> = ({ allDocTypes, show, onHide,
setName(e.target.value);
};

const handlePrefixChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (defaultDisabled) setDefaultDisabled(false);
setPrefix(e.target.value);
};

const handleCreatedByChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setCreatedBy(e.target.value);
};
Expand All @@ -69,6 +76,27 @@ const AddDocTypeModal: FC<AddDocTypeModalProps> = ({ allDocTypes, show, onHide,
<Form.Control type="text" onChange={handleNameChange} />
</Col>
</Form.Group>
<Form.Group as={Row} className="mb-3">
<Form.Label column sm="10">
File Prefix:
<OverlayTrigger
placement="right"
overlay={
<Tooltip id="button-tooltip-2">
The file prefix is the value added to the beginning of the generated document's filename.
</Tooltip>
}
>
<span className="ml-1" style={{ cursor: 'pointer' }}>
<i className="fas fa-info-circle"></i>
</span>
</OverlayTrigger>
</Form.Label>
<Col sm="10">
<Form.Control type="text" onChange={handlePrefixChange} />
</Col>
</Form.Group>

<Form.Group as={Row} className="mb-3">
<Form.Label column sm="10">
Created By:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const EditDocTypeTable: FC<EditDocTypeTableProps> = ({ refreshDocTypes }) => {
await updateDocType(
selectedDocType.id,
currentDocType.name,
currentDocType.prefix,
currentDocType.created_by,
currentDocType.created_date
);
Expand Down Expand Up @@ -67,7 +68,21 @@ const EditDocTypeTable: FC<EditDocTypeTableProps> = ({ refreshDocTypes }) => {
),
header: () => 'Document Type Name',
enableSorting: false,
meta: { customCss: { width: '17%' }, type: 'text' },
meta: { customCss: { width: '24%' }, type: 'text' },
}),
columnHelper.accessor('prefix', {
id: 'prefix',
cell: ({ row }) => (
<TableCell
getValue={() => row.original.prefix}
columnId="prefix"
onUpdate={updateHandler}
isEditing={isEditing}
/>
),
header: () => 'File Prefix',
enableSorting: true,
meta: { customCss: { width: '12%' }, type: 'text' },
}),
columnHelper.accessor('created_date', {
id: 'created_date',
Expand All @@ -81,7 +96,7 @@ const EditDocTypeTable: FC<EditDocTypeTableProps> = ({ refreshDocTypes }) => {
),
header: () => 'Date Created',
enableSorting: false,
meta: { customCss: { width: '17%', margin: '0px' }, type: 'text' },
meta: { customCss: { width: '12%', margin: '0px' }, type: 'text' },
}),
columnHelper.accessor('created_by', {
id: 'created_by',
Expand All @@ -95,7 +110,7 @@ const EditDocTypeTable: FC<EditDocTypeTableProps> = ({ refreshDocTypes }) => {
),
header: () => 'Created By',
enableSorting: false,
meta: { customCss: { width: '17%' }, type: 'text' },
meta: { customCss: { width: '12%' }, type: 'text' },
}),
columnHelper.accessor('update_timestamp', {
id: 'update_timestamp',
Expand All @@ -104,14 +119,14 @@ const EditDocTypeTable: FC<EditDocTypeTableProps> = ({ refreshDocTypes }) => {
),
header: () => 'Last Updated Date',
enableSorting: false,
meta: { customCss: { width: '17%' }, type: 'text' },
meta: { customCss: { width: '12%' }, type: 'text' },
}),
columnHelper.accessor('update_userid', {
id: 'update_userid',
cell: (info) => <input value={info.getValue()} className="form-control readonlyInput" readOnly />,
header: () => 'Last Updated By',
enableSorting: false,
meta: { customCss: { width: '17%' }, type: 'text' },
meta: { customCss: { width: '12%' }, type: 'text' },
}),
columnHelper.display({
id: 'edit_and_save',
Expand Down Expand Up @@ -156,9 +171,6 @@ interface TableCellProps<T> {
}

const TableCell: FC<TableCellProps<DocType>> = ({ getValue, columnId, onUpdate, isEditing }) => {
// const dispatch = useDispatch();
// const updatedDocType = useSelector((state: RootState) => state.docType.updatedDocType);
// const initialValue = getValue() ? (columnId === 'created_date' ? getValue().substring(0, 10) : getValue()) : '';
let initialValue = getValue() ? getValue() : '';
const [value, setValue] = useState(initialValue);

Expand All @@ -167,7 +179,6 @@ const TableCell: FC<TableCellProps<DocType>> = ({ getValue, columnId, onUpdate,
}, [initialValue]);

const handleBlur = () => {
// dispatch(setUpdatedDocType({ ...updatedDocType, [columnId]: value }));
onUpdate({ [columnId]: value });
};

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/app/content/pages/ManageDocumentsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ const ManageDocumentsPage: FC = () => {
setShowAddDocTypeModal(true);
};

const addDocTypeHandler = async (name: string, created_by: string, created_date: string) => {
const addDocTypeHandler = async (name: string, prefix: string, created_by: string, created_date: string) => {
try {
setLoading(true);
await addDocType(name, created_by, created_date);
await addDocType(name, prefix, created_by, created_date);
refreshDocTypes();
} catch (err) {
console.log('Error adding doc type');
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/app/store/reducers/docTypeSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const initialState: DocumentTypeObjectState = {
selectedDocType: {
id: -1,
name: '',
prefix: '',
created_by: '',
created_date: '',
create_userid: '',
Expand All @@ -22,6 +23,7 @@ const initialState: DocumentTypeObjectState = {
updatedDocType: {
id: -1,
name: '',
prefix: '',
created_by: '',
created_date: '',
create_userid: '',
Expand Down
1 change: 1 addition & 0 deletions frontend/src/app/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ export type SearchData = {
export type DocType = {
id: number;
name: string;
prefix: string;
created_by: string;
created_date: string;
create_userid: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ALTER TABLE document_type ADD COLUMN prefix VARCHAR(50) DEFAULT '';
UPDATE document_type SET prefix = 'Lease' WHERE name ILIKE 'Standard Lease';
UPDATE document_type SET prefix = 'Licence' WHERE name ILIKE 'Standard Licence';
UPDATE document_type SET prefix = 'Assumption' WHERE name ILIKE 'Assignment Assumption';
UPDATE document_type SET prefix = 'Modification' WHERE name ILIKE 'Modification Agreement';
UPDATE document_type SET prefix = 'NFR' WHERE name ILIKE 'Notice of Final Review%';
UPDATE document_type SET prefix = 'LUR' WHERE name ILIKE 'Land Use Report';
UPDATE document_type SET prefix = 'Lease' WHERE name ILIKE 'Grazing Lease';

0 comments on commit ae3eb93

Please sign in to comment.