Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#222] Add in Labels for Categorising People #348

Merged
merged 7 commits into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions backend/src/controllers/person.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ export const getPersonsByCompany = async (
res: Response,
next: NextFunction,
): Promise<void> => {

logger.info('GET /persons/companies/:id request from frontend');

const authId = req.headers.authorization?.['user_id'];
Expand All @@ -151,7 +150,7 @@ export const getPersonsByCompany = async (
async (personsId: any) => (await getPersonDetails(personsId)),
));
}

if (!company) {
res.status(httpStatus.NOT_FOUND).end();
} else {
Expand All @@ -163,6 +162,40 @@ export const getPersonsByCompany = async (
}
};

export const getPersonsByLabel = async (
req: Request,
res: Response,
next: NextFunction,
): Promise<void> => {
logger.info('GET /persons/label request from frontend');

const authId = req.headers.authorization?.['user_id'];
const user = await userService.getUserByAuthId(authId);
const reqLabel = req.query.label?.toString();

try {
if (!user) {
res.status(httpStatus.UNAUTHORIZED).end();
} else {
// get all the persons for this user
let people: any = await Promise.all(user.persons.map(
async (personsId: any) => (await personService.getPersonWithId(personsId)),
));

// filter, getting all people that have this label ( case insensitive )
let peopleWithLabel: any = people.filter((person) => person?.labels.some((label) => label.toLowerCase() === reqLabel?.toLowerCase()));

if (!peopleWithLabel) {
res.status(httpStatus.NOT_FOUND).end();
} else {
res.status(httpStatus.OK).json(peopleWithLabel).end();
}
}
} catch (e) {
next(e);
}
};

export const deletePersons = async (
req: Request,
res: Response,
Expand Down
2 changes: 2 additions & 0 deletions backend/src/models/person.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface PersonModel {
first_met: Date,
how_we_met: string,
interests: string[],
labels: string[],
organisation: string,
social_media: Map<string, string>,
image: Buffer,
Expand All @@ -32,6 +33,7 @@ const schema = new Schema<PersonModel>({
first_met: { type: Date, required: false },
how_we_met: { type: String, required: false },
interests: { type: [String], required: false },
labels: { type: [String], required: false },
organisation: { type: String, required: false },
social_media: { type: Map, of: String, required: false },
image: { type: Buffer, required: false },
Expand Down
2 changes: 2 additions & 0 deletions backend/src/routes/__test__/company.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const person1Data: PersonModel = {
first_name: 'Ping',
last_name: 'Pong',
interests: ['video games', 'hockey'],
labels: ['Devop'],
organisation: 'helloc',
time_updated: new Date('2022-01-01'),
importance_level: Importance.Very_Important,
Expand All @@ -65,6 +66,7 @@ const person2Data: PersonModel = {
first_name: 'Adam',
last_name: 'Bong',
interests: ['badminton', 'golf'],
labels: ['Devop'],
organisation: 'helloc',
time_updated: new Date('2022-02-23'),
importance_level: Importance.Should_Remember,
Expand Down
2 changes: 2 additions & 0 deletions backend/src/routes/__test__/encounter.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const person1Data: PersonModel = {
first_name: 'Ping',
last_name: 'Pong',
interests: ['video games', 'hockey'],
labels: ['Devop'],
organisation: 'helloc',
time_updated: new Date('2022-01-01'),
importance_level: Importance.Very_Important,
Expand All @@ -66,6 +67,7 @@ const person2Data: PersonModel = {
first_name: 'Adam',
last_name: 'Bong',
interests: ['badminton', 'golf'],
labels: ['Devop'],
organisation: 'helloc',
time_updated: new Date('2022-02-23'),
importance_level: Importance.Should_Remember,
Expand Down
48 changes: 48 additions & 0 deletions backend/src/routes/__test__/person.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const person1Data: PersonModel = {
first_name: 'Ping',
last_name: 'Pong',
interests: ['video games', 'hockey'],
labels: ['Devop'],
organisation: 'helloc',
time_updated: new Date('2022-01-01'),
importance_level: Importance.Very_Important,
Expand All @@ -56,6 +57,7 @@ const person2Data: PersonModel = {
first_name: 'Adam',
last_name: 'Bong',
interests: ['badminton', 'golf'],
labels: ['Devop'],
organisation: 'helloc',
time_updated: new Date('2022-02-23'),
importance_level: Importance.Should_Remember,
Expand All @@ -74,6 +76,7 @@ const person3Data: PersonModel = {
first_name: 'Billy',
last_name: 'John',
interests: ['surfing', 'cooking'],
labels: ['Devop'],
organisation: 'an organisation',
time_updated: new Date('2022-02-23'),
importance_level: Importance.Casual_Contact,
Expand All @@ -92,6 +95,7 @@ const person4Data: PersonModel = {
first_name: 'Kelvin',
last_name: 'Kong',
interests: ['Studying', 'Winning'],
labels: ['Devop'],
organisation: 'Winnie',
time_updated: new Date('2022-01-01'),
importance_level: Importance.Very_Important,
Expand All @@ -118,6 +122,7 @@ const userData: UserModel = {
const person5Data = {
last_name: 'John',
interests: ['surfing', 'cooking'],
labels: ['Devop'],
organisation: 'an organisation',
time_updated: new Date('2022-02-23'),
importance_level: Importance.Casual_Contact,
Expand All @@ -136,6 +141,7 @@ const person6Data = {
first_name: 'Billy',
last_name: 'John',
interests: ['surfing', 'cooking'],
labels: ['Devop'],
organisation: 'an organisation',
how_we_met: 'At the park',
birthday: new Date('2001-07-16'),
Expand Down Expand Up @@ -172,6 +178,7 @@ const person7Data = {
first_name: 'Yesterday',
last_name: 'Birthday',
interests: ['surfing', 'cooking'],
labels: [],
organisation: 'an organisation',
how_we_met: 'At the park',
birthday: new Date(new Date().getTime() - 24 * 60 * 60 * 1000),
Expand All @@ -187,6 +194,7 @@ const person8Data = {
first_name: 'Tomorrow',
last_name: 'Birthday',
interests: ['surfing', 'cooking'],
labels: ['Devop'],
organisation: 'an organisation',
how_we_met: 'At the park',
birthday: new Date(new Date().getTime() + 24 * 60 * 60 * 1000),
Expand All @@ -202,6 +210,7 @@ const person9Data = {
first_name: 'NextMonth',
last_name: 'Birthday',
interests: ['surfing', 'cooking'],
labels: ['Backend'],
organisation: 'an organisation',
how_we_met: 'At the park',
birthday: new Date(new Date().getTime() + 24 * 60 * 60 * 1000 * 30),
Expand Down Expand Up @@ -1029,6 +1038,45 @@ describe('GET /birthdays', () => {
.set('Accept', 'application/json')
.set('Authorization', token);

expect(people).toEqual({});
});
});

describe('GET /label', () => {

async function populateDbWithUsersPersons() {
const person1 = new Person(person7Data);
const person2 = new Person(person8Data);
const person3 = new Person(person9Data);

await person1.save();
await person2.save();
await person3.save();

userData.auth_id = await testUtils.getAuthIdFromToken(token);
const user = new User(userData);
user.persons.push(person1._id, person2._id, person3._id);
await user.save();

const storedPersonIds = [person1._id, person2._id, person3._id];

return storedPersonIds;
}

it('Returns correct number of entries', async () => {
await populateDbWithUsersPersons();
const { body: people } = await supertest(app).get('/api/persons/label?label=Devop')
.set('Accept', 'application/json')
.set('Authorization', token);

expect(people.length).toEqual(1);
});

it('Empty array is returned when there is none with the label', async () => {
const { body: people } = await supertest(app).get('/api/persons/label?label=frontend')
.set('Accept', 'application/json')
.set('Authorization', token);

expect(people).toEqual({});
});
});
2 changes: 2 additions & 0 deletions backend/src/routes/__test__/user.route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const person1Data: PersonModel = {
first_name: "Ray",
last_name: "Ping",
interests: ["video games", "hockey"],
labels: ['Devop'],
organisation: "helloc",
time_updated: new Date("2022-01-01"),
importance_level: null as any,
Expand All @@ -78,6 +79,7 @@ const person2Data: PersonModel = {
first_name: "Adam",
last_name: "Bong",
interests: ["badminton", "golf"],
labels: ['Devop'],
organisation: "helloc",
time_updated: new Date("2022-02-23"),
importance_level: Importance.Should_Remember,
Expand Down
3 changes: 2 additions & 1 deletion backend/src/routes/person.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
*/
import { Router } from 'express';
import {
createPerson, getAllPeople, getPersonWithId, updatePersonWithId, deletePersons, getPersonsByCompany,
createPerson, getAllPeople, getPersonWithId, updatePersonWithId, deletePersons, getPersonsByCompany, getPersonsByLabel,
} from '../controllers/person.controller';

const routes = Router();

routes.post('/', createPerson)
.get('/label', getPersonsByLabel)
.get('/:id', getPersonWithId)
.get('/', getAllPeople)
.get('/companies/:id', getPersonsByCompany)
Expand Down
1 change: 1 addition & 0 deletions backend/src/services/__test__/company.service.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const personOne: PersonModel = {
first_met: new Date('2012-12-21'),
how_we_met: "Life",
interests: ['things'],
labels: ['Devop'],
organisation: "World",
social_media: null as any,
image: null as any,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const person1Data: PersonModel = {
first_name: 'Ping',
last_name: 'Pong',
interests: ['video games', 'hockey'],
labels: ['Devop'],
organisation: 'helloc',
time_updated: new Date('2022-01-01'),
importance_level: Importance.Very_Important,
Expand Down
5 changes: 5 additions & 0 deletions backend/src/services/__test__/person.service.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const person1Data:PersonModel = {
first_name: 'testlname',
last_name: 'testllastName',
interests: ['a', 'b'],
labels: ['Devop'],
organisation: 'testorg',
time_updated: new Date('2022-01-01'),
importance_level: Importance.Very_Important,
Expand All @@ -31,6 +32,7 @@ const person1Data:PersonModel = {
first_name: 'test2name',
last_name: 'test2lastName',
interests: ['c', 'd'],
labels: ['Devop'],
organisation: 'anotherOrg',
time_updated: new Date('2022-01-01'),
importance_level: Importance.Should_Remember,
Expand All @@ -49,6 +51,7 @@ const person1Data:PersonModel = {
first_name: 'test3name',
last_name: 'test3lastName',
interests: ['c', 'd'],
labels: ['Devop'],
organisation: 'anotherOrg',
time_updated: new Date('2022-01-01'),
importance_level: Importance.Casual_Contact,
Expand All @@ -67,6 +70,7 @@ const person1Data:PersonModel = {
first_name: null as any,
last_name: 'testllastName',
interests: ['a', 'b'],
labels: ['Devop'],
organisation: 'testorg',
time_updated: new Date('2022-01-01'),
importance_level: Importance.Very_Important,
Expand All @@ -85,6 +89,7 @@ const person1Data:PersonModel = {
first_name: 'testfirstname',
last_name: 'testllastName',
interests: ['a', 'b'],
labels: ['Devop'],
organisation: 'testorg',
time_updated: null as any,
importance_level: Importance.Casual_Contact,
Expand Down
12 changes: 12 additions & 0 deletions forgettable-frontend/src/components/PersonCard/PersonCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import IconButton from '@mui/material/IconButton';
import Menu from '@mui/material/Menu';
import MenuItem from '@mui/material/MenuItem';
import MoreHorizIcon from '@mui/icons-material/MoreHoriz';
import Chip from '@mui/material/Chip';
import Box from '@mui/material/Box';
import {convertSocialMediaToIcon} from '../../functions/socialMediaIconConverter';
import PropTypes from 'prop-types';
import {getFirstMetTimeString, getLongDateStringWithSlashes} from '../../functions/dateFormatter';
Expand Down Expand Up @@ -133,6 +135,16 @@ const PersonCard = (props) => {
)}
</Menu>
</div>
{props.label &&
<div>
<Box sx={{display: 'flex', flexWrap: 'wrap', gap: 1}}>
{props.labels.map((label) => {
return (
<Chip key={label} label={label} />
);
})}
</Box>
</div>}
<div className={classes.SupplementaryInformationContainer}>
<p className={classes.Encounters}
data-testid="encounters-element"
Expand Down
10 changes: 10 additions & 0 deletions forgettable-frontend/src/components/PersonDrawer/PersonDrawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
import {capitalise} from '../../functions/stringFormatter';
import convertSocialMediaNamesToIcons,
{convertSocialMediaToIcon} from '../../functions/socialMediaIconConverter';
import Chip from '@mui/material/Chip';
import Box from '@mui/material/Box';
import {IconButton} from '@mui/material';
import {getLongDateStringWithSpaces} from '../../functions/dateFormatter';
import CustomButton from '../CustomButton/CustomButton';
Expand Down Expand Up @@ -161,6 +163,14 @@ const PersonDrawer = (props) => {
})}
</span> :<UnknownDetail/>}
</p>
<p>
{'Labels: '}
{props.labels && props.labels.length > 0 ?
<span className={classes.KnownText}>
{props.labels.join(', ')}
</span> :
<UnknownDetail/>}
</p>
{props.staticDrawer &&
<CustomButton
btnText="Edit"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ exports[`renders PersonDrawer UI with correct hierarchy 1`] = `
</ForwardRef(IconButton)>
</span>
</p>
<p>
Labels:
<UnknownDetail />
</p>
</div>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions forgettable-frontend/src/pages/PersonPage/PersonPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ const PersonPage = (props) => {
socialMedia={person.socialMedia}
data-testid="drawer-component"
onEdit={() => navigate(`/person/${id}/edit`)}
labels={person.labels}
/>
<div className={classes.ContentContainer}>
<div className={classes.TitleContainer} >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export default function PersonsListPage(props) {
firstMet={selectedInfo.firstMet}
location={selectedInfo.location}
interests={selectedInfo.interests}
labels={selectedInfo.labels}
/>
}
<CustomModal
Expand Down Expand Up @@ -216,6 +217,7 @@ export default function PersonsListPage(props) {
onDelete={(e) => onDeletePersonCardClicked(e, person._id)}
firstMet= {person.first_met}
image={person.image}
labels={person.labels}
/>
</div>
);
Expand Down
Loading