-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
51 changed files
with
1,135 additions
and
263 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...ntral-server/src/apiV2/userFavouriteDashboardItem/POSTUpdateUserFavouriteDashboardItem.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2022 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import { respond } from '@tupaia/utils'; | ||
import { assertAnyPermissions } from '../../permissions'; | ||
import { CRUDHandler } from '../CRUDHandler'; | ||
|
||
/** | ||
* Handles POST endpoints: | ||
* - /userFavouriteDashboardItem | ||
*/ | ||
|
||
export class POSTUpdateUserFavouriteDashboardItem extends CRUDHandler { | ||
async assertUserHasAccess() { | ||
const assertUserIsLoggedIn = () => { | ||
const { userId } = this.req; | ||
if (!userId) { | ||
throw new Error('User should be logged in'); | ||
} | ||
}; | ||
|
||
return this.assertPermissions(assertAnyPermissions([assertUserIsLoggedIn])); | ||
} | ||
|
||
async handleRequest() { | ||
const { models, body, userId } = this.req; | ||
const { dashboardItemCode, state } = body; | ||
|
||
const user = await models.user.findOne({ id: userId }); | ||
const dashboardItemCodeToId = await models.dashboardItem.findIdByCode(dashboardItemCode); | ||
const dashboardItemId = dashboardItemCodeToId[dashboardItemCode]; | ||
if (!user || !dashboardItemId) { | ||
throw new Error(`user or dashboard item not found`); | ||
} | ||
|
||
if (state === 'favourite') { | ||
await models.userFavouriteDashboardItem.favourite(userId, dashboardItemId); | ||
} | ||
if (state === 'unfavourite') { | ||
await models.userFavouriteDashboardItem.unfavourite(userId, dashboardItemId); | ||
} | ||
|
||
respond(this.res, { message: 'successfully modified' }); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
packages/central-server/src/apiV2/userFavouriteDashboardItem/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './POSTUpdateUserFavouriteDashboardItem'; |
41 changes: 41 additions & 0 deletions
41
.../database/src/migrations/20220823234713-AddUserFavouriteDashboardTable-modifies-schema.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
'use strict'; | ||
|
||
var dbm; | ||
var type; | ||
var seed; | ||
|
||
/** | ||
* We receive the dbmigrate dependency from dbmigrate initially. | ||
* This enables us to not have to rely on NODE_PATH. | ||
*/ | ||
exports.setup = function (options, seedLink) { | ||
dbm = options.dbmigrate; | ||
type = dbm.dataType; | ||
seed = seedLink; | ||
}; | ||
|
||
exports.up = function (db) { | ||
return db.runSql(` | ||
CREATE TABLE user_favourite_dashboard_item ( | ||
id TEXT PRIMARY KEY, | ||
user_id TEXT NOT NULL, | ||
dashboard_item_id TEXT NOT NULL, | ||
UNIQUE (user_id, dashboard_item_id), | ||
FOREIGN KEY (user_id) REFERENCES user_account (id) ON UPDATE CASCADE ON DELETE CASCADE, | ||
FOREIGN KEY (dashboard_item_id) REFERENCES dashboard_item (id) ON UPDATE CASCADE ON DELETE CASCADE | ||
); | ||
CREATE INDEX user_favourite_dashboard_item_user_id_idx ON user_favourite_dashboard_item USING btree (user_id); | ||
`); | ||
}; | ||
|
||
exports.down = function (db) { | ||
return db.runSql(` | ||
DROP TABLE public.user_favourite_dashboard_item; | ||
`); | ||
}; | ||
|
||
exports._meta = { | ||
version: 1, | ||
}; |
36 changes: 36 additions & 0 deletions
36
packages/database/src/modelClasses/UserFavouriteDashboardItem.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Tupaia | ||
* Copyright (c) 2017 - 2022 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import { DatabaseModel } from '../DatabaseModel'; | ||
import { DatabaseType } from '../DatabaseType'; | ||
import { TYPES } from '../types'; | ||
|
||
class UserFavouriteDashboardItemType extends DatabaseType { | ||
static databaseType = TYPES.USER_FAVOURITE_DASHBOARD_ITEM; | ||
|
||
static joins = [ | ||
{ | ||
fields: { | ||
code: 'dashboard_item_code', | ||
}, | ||
joinWith: TYPES.DASHBOARD_ITEM, | ||
joinCondition: ['dashboard_item.id', 'user_favourite_dashboard_item.dashboard_item_id'], | ||
}, | ||
]; | ||
} | ||
|
||
export class UserFavouriteDashboardItemModel extends DatabaseModel { | ||
get DatabaseTypeClass() { | ||
return UserFavouriteDashboardItemType; | ||
} | ||
|
||
async favourite(userId, dashboardItemId) { | ||
return this.findOrCreate({ dashboard_item_id: dashboardItemId, user_id: userId }); | ||
} | ||
|
||
async unfavourite(userId, dashboardItemId) { | ||
return this.delete({ dashboard_item_id: dashboardItemId, user_id: userId }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
packages/lesmis/src/api/mutations/useUpdateFavouriteDashboardItem.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Tupaia | ||
* Copyright (c) 2017 - 2022 Beyond Essential Systems Pty Ltd | ||
*/ | ||
import { useMutation, useQueryClient } from 'react-query'; | ||
import { post } from '../api'; | ||
|
||
const updateExistingDashboardData = async (queryClient, { entityCode }) => { | ||
return queryClient.refetchQueries(['dashboard', entityCode]); | ||
}; | ||
|
||
const useFavouriteDashboardItem = queryClient => { | ||
return useMutation( | ||
({ dashboardItemCode }) => | ||
post('userFavouriteDashboardItems', { | ||
data: { | ||
state: 'favourite', | ||
dashboardItemCode, | ||
}, | ||
}), | ||
{ | ||
onSuccess: async (data, variables) => { | ||
return updateExistingDashboardData(queryClient, variables); | ||
}, | ||
}, | ||
); | ||
}; | ||
|
||
const useUnfavouriteDashboardItem = queryClient => | ||
useMutation( | ||
({ dashboardItemCode }) => | ||
post('userFavouriteDashboardItems', { | ||
data: { | ||
state: 'unfavourite', | ||
dashboardItemCode, | ||
}, | ||
}), | ||
{ | ||
onSuccess: async (data, variables) => { | ||
return updateExistingDashboardData(queryClient, variables); | ||
}, | ||
}, | ||
); | ||
|
||
export const useUpdateFavouriteDashboardItem = () => { | ||
const queryClient = useQueryClient(); | ||
const favouriteMutation = useFavouriteDashboardItem(queryClient); | ||
const unfavouriteMutation = useUnfavouriteDashboardItem(queryClient); | ||
|
||
const mutate = ({ newFavouriteStatus, dashboardItemCode, entityCode }) => { | ||
const mutation = newFavouriteStatus ? favouriteMutation : unfavouriteMutation; | ||
mutation.mutate({ dashboardItemCode, entityCode }); | ||
}; | ||
|
||
return mutate; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.