Skip to content

Commit

Permalink
implement getting all logs for one person. Each log currently has an …
Browse files Browse the repository at this point in the history
…account instance attached, as per the TODO
  • Loading branch information
SillyFreak committed Aug 3, 2020
1 parent d2307a2 commit 7410d30
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion models/index.js
Expand Up @@ -25,7 +25,7 @@ const Log = sequelize.import('./log.model');
const Plugin = sequelize.import('./plugin.model');

Account.belongsTo(Plugin);
Account.hasMany(Log);
Log.belongsTo(Account);

export { Account, Log, Plugin };

Expand Down
15 changes: 12 additions & 3 deletions server/controller/log.controller.js
@@ -1,7 +1,7 @@
import httpErrors from 'httperrors';
import { Op } from 'sequelize';

import { Log } from '../../models';
import { Account, Log } from '../../models';

export async function create({ accountUuid, nativeLocation }) {
// validate data
Expand All @@ -22,14 +22,23 @@ export async function create({ accountUuid, nativeLocation }) {
}
}

export async function readAll({ accountUuid }) {
export async function readAll({ accountUuid, personUuid }) {
// create filter
const condition = {};
const include = [];
if (accountUuid) condition.accountUuid = accountUuid;
if (personUuid) {
// TODO this adds the account to the fetched data,
// even though we want the account only for filtering
include.push({
model: Account,
where: { personUuid },
});
}

// query database
try {
const log = await Log.findAll({ where: condition });
const log = await Log.findAll({ where: condition, include });
return log;
} catch (err) {
throw new httpErrors[500](err.message || 'An error occurred...');
Expand Down
15 changes: 15 additions & 0 deletions test/controller/log.spec.js
Expand Up @@ -65,6 +65,21 @@ describe('Log Controller', () => {
]);
}

// read all by person
{
const logs = await Log.readAll({ personUuid });
// toMatchObject because sequelize model instances are not plain objects
expect(logs).toMatchObject([
{
id: expect.any(Number),
accountUuid,
createdAt: expect.any(Date),
updatedAt: expect.any(Date),
nativeLocation: 'foo',
},
]);
}

// update
{
await Log.update(id, {
Expand Down

0 comments on commit 7410d30

Please sign in to comment.