Skip to content

Commit f62ff9b

Browse files
sarimabbasbpalomino5
authored andcommitted
added Roles to database
Added compensation and hours fields Added role management to Project
1 parent 2b13874 commit f62ff9b

File tree

4 files changed

+170
-56
lines changed

4 files changed

+170
-56
lines changed

src/firebase/models/Project.js

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -72,52 +72,28 @@ export const destroy = async (projectId) => {
7272
* @param {String} projectId
7373
* @param {String} type : Retrieve only tags of a certain type.
7474
*/
75-
export const getTags = async (projectId, type) => {
76-
const data = await get(projectId);
77-
if (isNil(data.tags)) {
78-
return [];
79-
}
80-
const tags = await _shared.getFromIdsArray('tags', data.tags);
81-
if (type !== undefined) {
82-
return tags.filter(tag => tag.type === type);
83-
}
84-
return tags;
85-
};
75+
export const getTags = (projectId, type) => _shared.getTags('projects', projectId, type);
8676

8777
/**
8878
* Associate tag with project.
8979
* @param {String} projectId
9080
* @param {String} tagId
9181
*/
92-
export const addTag = (projectId, tagId) => _shared.addToSet('projects', projectId, 'tags', tagId);
82+
export const addTag = (projectId, tagId) => _shared.addTag('projects', projectId, tagId);
9383

9484
/**
9585
* Dissociate tag with project.
9686
* @param {String} projectId
9787
* @param {String} tagId
9888
*/
99-
export const removeTag = (projectId, tagId) => _shared.removeFromSet('projects', projectId, 'tags', tagId);
89+
export const removeTag = (projectId, tagId) => _shared.removeTag('projects', projectId, tagId);
10090

10191
/**
10292
* Drop all tags for the project, or some.
10393
* @param {String} projectId
10494
* @param {String} type : Drop only tags of this certain type.
10595
*/
106-
export const dropTags = async (projectId, type = undefined) => {
107-
// read the project
108-
const data = await get(projectId);
109-
if (isNil(data.tags)) {
110-
return [];
111-
}
112-
// if no type specified, remove all tags
113-
if (type === undefined) {
114-
return update(projectId, { tags: [] });
115-
}
116-
// otherwise, remove only those tags belonging to the type
117-
const tags = await _shared.getFromIdsArray('tags', data.tags);
118-
const filteredTags = tags.filter(tag => tag.type !== type);
119-
return update(projectId, { tags: filteredTags.map(tag => tag.id) });
120-
};
96+
export const dropTags = (projectId, type = undefined) => _shared.dropTags('projects', projectId, type);
12197

12298
// team management
12399

@@ -188,3 +164,19 @@ export const removeAdminUser = (projectId, userId) => _shared.removeFromSet('pro
188164
* @param {String} projectId
189165
*/
190166
export const dropAdmin = projectId => update(projectId, { admin: [] });
167+
168+
// role management
169+
170+
/**
171+
* Associate a role with a project.
172+
* @param {String} projectId
173+
* @param {String} roleId
174+
*/
175+
export const addRole = (projectId, roleId) => _shared.addToSet('projects', projectId, 'roles', roleId);
176+
177+
/**
178+
* Dissociate a role with a project.
179+
* @param {String} projectId
180+
* @param {String} roleId
181+
*/
182+
export const removeRole = (projectId, roleId) => _shared.removeFromSet('projects', projectId, 'roles', roleId);

src/firebase/models/Roles.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import defaultsDeep from 'lodash/defaultsDeep';
2+
import * as _shared from './shared';
3+
4+
// general CRUD stuff
5+
6+
/**
7+
* Creates a new document in the database.
8+
* @param {Object} props
9+
* @returns {String} New/created document id.
10+
*/
11+
export const create = async (props) => {
12+
const newId = _shared.getNewId('roles');
13+
// eslint-disable-next-line object-curly-newline
14+
const finalProps = defaultsDeep(props, {
15+
postedBy: '', // ID of Project or Organization
16+
name: '', // e.g. Sales Intern
17+
// Markdown will help cater to a variety of requirements and is safer than
18+
// HTML. Although people will be able to type whatever they like, the UI
19+
// will cue them to separate their writing into logical sections. Links
20+
// and images will be embedded within the Markdown using some sort of
21+
// editor plugin
22+
description: '', // overview of the position in Markdown
23+
responsibilities: '', // specific responsibilities in Markdown
24+
more: '', // more content in Markdown
25+
isCompensated: true,
26+
hourRange: '', // how long they will work per week/day?
27+
dateRange: '', // start and end dates for work
28+
tags: [], // String[] ids
29+
// eslint-disable-next-line object-curly-newline
30+
});
31+
return _shared.create('roles', newId, finalProps);
32+
};
33+
34+
/**
35+
*
36+
* @param {String} roleId
37+
* @returns {Object}
38+
*/
39+
export const get = roleId => _shared.get('roles', roleId);
40+
41+
/**
42+
*
43+
* @param {String} roleId
44+
* @param {Object} props
45+
*/
46+
export const update = (roleId, props) => _shared.update('roles', roleId, props);
47+
48+
/**
49+
* Deletes all role assets and deletes database record.
50+
* @param {String} roleId
51+
*/
52+
export const destroy = roleId => _shared.destroy('roles', roleId);
53+
54+
// tag management
55+
56+
/**
57+
* Retrieve all tags for a role.
58+
* @param {String} roleId
59+
* @param {String} type : Retrieve only tags of a certain type.
60+
*/
61+
export const getTags = (roleId, type) => _shared.getTags('roles', roleId, type);
62+
63+
/**
64+
* Associate tag with role.
65+
* @param {String} roleId
66+
* @param {String} tagId
67+
*/
68+
export const addTag = (roleId, tagId) => _shared.addTag('roles', roleId, tagId);
69+
70+
/**
71+
* Dissociate tag with role.
72+
* @param {String} roleId
73+
* @param {String} tagId
74+
*/
75+
export const removeTag = (roleId, tagId) => _shared.removeTag('roles', roleId, tagId);
76+
77+
/**
78+
* Drop all tags for the role, or some.
79+
* @param {String} roleId
80+
* @param {String} type : Drop only tags of this certain type.
81+
*/
82+
export const dropTags = (roleId, type = undefined) => _shared.dropTags('roles', roleId, type);

src/firebase/models/User.js

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,43 +63,25 @@ export const destroy = userId => _shared.destroy('users', userId);
6363
* @param {String} userId
6464
* @param {String} type : Retrieve only tags of a certain type.
6565
*/
66-
export const getTags = async (userId, type) => {
67-
const data = await get(userId);
68-
const tags = await _shared.getFromIdsArray(data.tags);
69-
// filter if necessary
70-
if (type !== undefined) {
71-
return tags.filter(tag => tag.type === type);
72-
}
73-
return tags;
74-
};
66+
export const getTags = (userId, type) => _shared.getTags('users', userId, type);
7567

7668
/**
7769
* Associate tag with user.
7870
* @param {String} userId
7971
* @param {String} tagId
8072
*/
81-
export const addTag = (userId, tagId) => _shared.addToSet('users', userId, 'tags', tagId);
73+
export const addTag = (userId, tagId) => _shared.addTag('users', userId, tagId);
8274

8375
/**
8476
* Dissociate tag with user.
8577
* @param {String} userId
8678
* @param {String} tagId
8779
*/
88-
export const removeTag = (userId, tagId) => _shared.removeFromSet('users', userId, 'tags', tagId);
80+
export const removeTag = (userId, tagId) => _shared.removeTag('users', userId, tagId);
8981

9082
/**
9183
* Drop all tags for the user, or some.
9284
* @param {String} userId
9385
* @param {String} type : Drop only tags of this certain type.
9486
*/
95-
export const dropTags = async (userId, type = undefined) => {
96-
const data = await get(userId);
97-
// if no type, then remove all tags
98-
if (type === undefined) {
99-
return update(userId, { tags: [] });
100-
}
101-
// otherwise remove only a subset of tags
102-
const tags = await _shared.getFromIdsArray(data.tags);
103-
const filteredTags = tags.filter(tag => tag.type !== type);
104-
return update(userId, { tags: filteredTags.map(tag => tag.id) });
105-
};
87+
export const dropTags = (userId, type = undefined) => _shared.dropTags('users', userId, type);

src/firebase/models/shared.js

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import defaultsDeep from 'lodash/defaultsDeep';
2+
import isNil from 'lodash/isNil';
23
import { db, createTimestamp, FieldValue } from '../firebase';
34

45
// identity management
@@ -34,11 +35,7 @@ export const getIdFromRef = ref => ref.id;
3435
*/
3536
export const create = async (path, id, props) => {
3637
const ref = getRefFromPathId(path, id);
37-
// eslint-disable-next-line object-curly-newline
38-
const finalProps = defaultsDeep(props, {
39-
createdTimestamp: createTimestamp(new Date()),
40-
// eslint-disable-next-line object-curly-newline
41-
});
38+
const finalProps = defaultsDeep(props, { createdTimestamp: createTimestamp(new Date()) });
4239
await ref.set(finalProps, { merge: true });
4340
return id;
4441
};
@@ -50,7 +47,10 @@ export const create = async (path, id, props) => {
5047
* @param {Object} props : Properties.
5148
* @returns {Promise}
5249
*/
53-
export const update = (path, id, props) => getRefFromPathId(path, id).update(props);
50+
export const update = (path, id, props) => {
51+
const finalProps = defaultsDeep(props, { updatedTimestamp: createTimestamp(new Date()) });
52+
return getRefFromPathId(path, id).update(finalProps);
53+
}
5454

5555
/**
5656
*
@@ -121,3 +121,61 @@ export const getFromQuery = async (query) => {
121121
return querySnapshot.docs.map(queryDocumentSnapshot => ({ ...queryDocumentSnapshot.data(),
122122
id: queryDocumentSnapshot.id }));
123123
};
124+
125+
// tag management
126+
127+
/**
128+
* Retrieve all tags for a document, irrespective of whether it has a tags field.
129+
* @param {String} path
130+
* @param {String} id
131+
* @param {String} type : Retrieve only tags of a certain type.
132+
*/
133+
export const getTags = async (path, id, type) => {
134+
const data = await get(path, id);
135+
if (isNil(data.tags)) {
136+
return [];
137+
}
138+
const tags = await getFromIdsArray('tags', data.tags);
139+
if (type !== undefined) {
140+
return tags.filter(tag => tag.type === type);
141+
}
142+
return tags;
143+
};
144+
145+
/**
146+
* Associate tag with document, irrespective of whether it has a tags field.
147+
* @param {String} path
148+
* @param {String} id
149+
* @param {String} tagId
150+
*/
151+
export const addTag = (path, id, tagId) => addToSet(path, id, 'tags', tagId);
152+
153+
/**
154+
* Dissociate tag with document, irrespective of whether it has a tags field.
155+
* @param {String} path
156+
* @param {String} id
157+
* @param {String} tagId
158+
*/
159+
export const removeTag = (path, id, tagId) => removeFromSet(path, id, 'tags', tagId);
160+
161+
/**
162+
* Drop all tags for the document, or some, irrespective of whether it has a tags field.
163+
* @param {String} path
164+
* @param {String} id
165+
* @param {String} type : Drop only tags of this certain type.
166+
*/
167+
export const dropTags = async (path, id, type = undefined) => {
168+
// read the document
169+
const data = await get(path, id);
170+
if (isNil(data.tags)) {
171+
return [];
172+
}
173+
// if no type specified, remove all tags
174+
if (type === undefined) {
175+
return update(path, id, { tags: [] });
176+
}
177+
// otherwise, remove only those tags belonging to the type
178+
const tags = await getFromIdsArray('tags', data.tags);
179+
const filteredTags = tags.filter(tag => tag.type !== type);
180+
return update(path, id, { tags: filteredTags.map(tag => tag.id) });
181+
};

0 commit comments

Comments
 (0)