Skip to content

Commit 88ad780

Browse files
authored
Merge pull request #45 from icefoganalytics/caleb/dev
ytgov#165 - Fix dup `yHSIId` bug and allow administrators to edit `yHSIId` of a place(site)
2 parents a04211e + 34be1ae commit 88ad780

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

api/routes/place-router.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ placeRouter.patch(
268268
authorize([UserRoles.SITE_ADMIN, UserRoles.ADMINISTRATOR]),
269269
[
270270
param('id').isInt({ gt: 0 }),
271+
body('yHSIId').isString().optional({ nullable: true }),
271272
body('associations').isArray().optional({ nullable: true }),
272273
body('block').isString().optional({ nullable: true }),
273274
body('bordenNumber').isString().optional({ nullable: true }),
@@ -343,6 +344,16 @@ placeRouter.patch(
343344
locations: ['body'],
344345
});
345346

347+
// Should be using BaseController and loading currentUser into req here
348+
const user = req.user as User;
349+
if (isNil(user) || isNil(user.roles)) {
350+
return res.status(401).json({ message: 'Unauthorized' });
351+
}
352+
353+
if (!user.roles.includes(UserRoles.ADMINISTRATOR)) {
354+
attributes.yHSIId = undefined;
355+
}
356+
346357
return placeService
347358
.update(id, attributes)
348359
.then((data) => {

api/services/place-service.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import knex, { Knex } from 'knex';
22
import moment from 'moment';
3-
import { get, isEmpty, isNil, isNull, uniq } from 'lodash';
4-
import { COORDINATE_DETERMINATION_TYPES, SITE_STATUS_TYPES } from '../models';
3+
import { get, isEmpty, isNull, isUndefined, uniq } from 'lodash';
4+
import { COORDINATE_DETERMINATION_TYPES } from '../models';
55

66
import {
77
AssociationService,
@@ -302,27 +302,26 @@ export class PlaceService {
302302
}
303303

304304
async generateIdFor(nTSMapSheet: string): Promise<string> {
305-
let placeIndex = 0;
306-
307305
const places = await this.db('place')
308306
.whereILike('YHSIId', `${nTSMapSheet}/%`)
309307
.select('YHSIId')
310308
.orderBy('yHSIId');
311309

312-
for (const place of places) {
313-
placeIndex++;
314-
315-
const testValue = `${nTSMapSheet}/${placeIndex.toString().padStart(3, '0')}`;
316-
317-
const isMatch = place.YHSIId == testValue;
310+
// Note: The following loop runs in O(999 * n) (or O(n) where n is the number of places)
311+
// If performance becomes an issue as number of places grows, consider using a Set
312+
let placeIndex = 1;
313+
for (; placeIndex <= 999; placeIndex++) {
314+
const candidateYHSIId = `${nTSMapSheet}/${placeIndex.toString().padStart(3, '0')}`;
315+
const isMatch = places.find((place) => place.YHSIId == candidateYHSIId);
318316

319-
if (!isMatch) {
320-
break;
317+
if (isUndefined(isMatch)) {
318+
return `${nTSMapSheet}/${placeIndex.toString().padStart(3, '0')}`;
321319
}
322320
}
323321

324-
placeIndex++;
325-
return `${nTSMapSheet}/${placeIndex.toString().padStart(3, '0')}`;
322+
throw new Error(
323+
`Unable to generate YHSIId: ${nTSMapSheet}/001 to ${nTSMapSheet}/999 are currently in use`
324+
);
326325
}
327326

328327
async doSearch(

web/src/components/Sites/site-forms/Summary.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
outlined
2121
label="YHSI ID"
2222
required
23-
append-icon="mdi-lock"
24-
:readonly="!isEditing"
23+
:append-icon="isSystemAdmin ? '' : 'mdi-lock'"
24+
:readonly="!isSystemAdmin || !isEditing"
2525
/>
2626

2727
<v-text-field
@@ -237,6 +237,7 @@ export default {
237237
computed: {
238238
...mapGetters({
239239
place: 'places/place',
240+
isSystemAdmin: 'users/isSystemAdmin',
240241
}),
241242
isEditing() {
242243
return this.$route.path.includes('/edit');

web/src/store/users.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from "axios";
2+
import { UserRoles } from '@/authorization';
23
import { COMMUNITY_URL, FIRST_NATION_URL, USER_URL } from "../urls";
34

45
const state = {
@@ -10,6 +11,7 @@ const getters = {
1011
roles: state => state.roles,
1112
communities: state => state.communities,
1213
firstNations: state => state.firstNations,
14+
isSystemAdmin: state => state.roles.includes(UserRoles.ADMINISTRATOR),
1315
};
1416
const mutations = {
1517
setRoles(state, value) {
@@ -91,4 +93,4 @@ export default {
9193
getters,
9294
mutations,
9395
actions
94-
};
96+
};

0 commit comments

Comments
 (0)