-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/configurable-widgets' into develop
- Loading branch information
Showing
42 changed files
with
803 additions
and
276 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,35 +1,17 @@ | ||
import { Column, Entity, JoinColumn, OneToOne, PrimaryGeneratedColumn } from 'typeorm'; | ||
import { Column, Entity, JoinColumn, ManyToOne, OneToOne, PrimaryGeneratedColumn } from 'typeorm'; | ||
import { User } from './User'; | ||
|
||
export enum DashboardWidgets { | ||
THIS_MOTH = 'THIS_MONTH', | ||
THIS_YEAR = 'THIS_YEAR', | ||
CURRENT_STATUS = 'CURRENT_STATUS', | ||
LATEST_RECORDS = 'LATEST_RECORDS', | ||
MONTHLY_CATEGORY = 'MONTHLY_CATEGORY', | ||
QUICK_ACTIONS = 'QUICK_ACTIONS', | ||
} | ||
|
||
@Entity() | ||
export class DashboardSettings { | ||
@PrimaryGeneratedColumn('uuid') | ||
id: string; | ||
@Column({ | ||
type: 'simple-array', | ||
enum: DashboardWidgets, | ||
default: [ | ||
DashboardWidgets.THIS_MOTH, | ||
DashboardWidgets.THIS_YEAR, | ||
DashboardWidgets.CURRENT_STATUS, | ||
DashboardWidgets.LATEST_RECORDS, | ||
DashboardWidgets.MONTHLY_CATEGORY, | ||
DashboardWidgets.QUICK_ACTIONS, | ||
], | ||
}) | ||
enabledWidgets: DashboardWidgets[]; | ||
@Column() | ||
widget: string; | ||
@Column() | ||
order: number; | ||
@Column() | ||
ownerUsername: string; | ||
@OneToOne(() => User) | ||
@ManyToOne(() => User) | ||
@JoinColumn({ name: 'ownerUsername' }) | ||
owner: User; | ||
} |
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
22 changes: 0 additions & 22 deletions
22
backend/src/migration/1636302462708-AddDashboardSettings.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
import { DEFAULT_WIDGETS } from '../settings/models/Settings'; | ||
import { services } from '../shared/services/services'; | ||
|
||
export class DashboardSettings1648214448116 implements MigrationInterface { | ||
name = 'DashboardSettings1648214448116'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`CREATE TABLE \`dashboard_settings\` (\`id\` varchar(36) NOT NULL, \`widget\` varchar(255) NOT NULL, \`order\` int NOT NULL, \`ownerUsername\` varchar(255) NOT NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE \`dashboard_settings\` ADD CONSTRAINT \`FK_44eca2a73ba5b456b2a9509e390\` FOREIGN KEY (\`ownerUsername\`) REFERENCES \`user\`(\`username\`) ON DELETE NO ACTION ON UPDATE NO ACTION`, | ||
); | ||
|
||
const users = await services.userService.getAllUsers(); | ||
for (const user of users) { | ||
await services.settingsService.updateWidgets(user.username, DEFAULT_WIDGETS); | ||
} | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE \`dashboard_settings\` DROP FOREIGN KEY \`FK_44eca2a73ba5b456b2a9509e390\``, | ||
); | ||
await queryRunner.query(`DROP TABLE \`dashboard_settings\``); | ||
} | ||
} |
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,21 @@ | ||
import { services } from '../../shared/services/services'; | ||
import { AvailableWidgets, UserSettings } from '../models/Settings'; | ||
import { SettingsController } from '../models/SettingsController'; | ||
|
||
const SETTINGS_CONTROLLER: SettingsController = { | ||
getUserSettings: async (ctx) => { | ||
const settings = await services.settingsService.getUserWidgets(ctx.state.user.username); | ||
const userSettings: UserSettings = { widgets: settings.map((setting) => setting.widget as AvailableWidgets) }; | ||
return { data: userSettings, status: 200 }; | ||
}, | ||
updateSettings: async (ctx) => { | ||
const { widgets } = ctx.request.body; | ||
const updatedSettings = await services.settingsService.updateWidgets(ctx.state.user.username, widgets); | ||
const updatedUserSettings: UserSettings = { | ||
widgets: updatedSettings.map((setting) => setting.widget), | ||
}; | ||
return { data: updatedUserSettings, status: 200 }; | ||
}, | ||
}; | ||
|
||
export default SETTINGS_CONTROLLER; |
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,28 @@ | ||
export const DEFAULT_WIDGETS: AvailableWidgets[] = [ | ||
'general-header', | ||
'quick-actions', | ||
'month-picker', | ||
'overview-header', | ||
'month-status', | ||
'category-data', | ||
'current-status', | ||
'daily-records', | ||
'historical-data-header', | ||
'latest-records', | ||
'this-year', | ||
]; | ||
|
||
export type AvailableWidgets = | ||
| 'general-header' | ||
| 'quick-actions' | ||
| 'month-picker' | ||
| 'overview-header' | ||
| 'month-status' | ||
| 'category-data' | ||
| 'current-status' | ||
| 'daily-records' | ||
| 'historical-data-header' | ||
| 'latest-records' | ||
| 'this-year'; | ||
|
||
export type UserSettings = { widgets: AvailableWidgets[] }; |
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,7 @@ | ||
import { ControllerFunction } from '../../shared/models/BaseController'; | ||
import { UserSettings } from './Settings'; | ||
|
||
export interface SettingsController { | ||
getUserSettings: ControllerFunction<UserSettings>; | ||
updateSettings: ControllerFunction<UserSettings>; | ||
} |
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,18 @@ | ||
import { User } from '../../entity/User'; | ||
import { repositories } from '../../shared/repositories/database'; | ||
import { AvailableWidgets } from '../models/Settings'; | ||
|
||
export default class SettingsService { | ||
getUserWidgets(username: User['username']) { | ||
return repositories.settings().find({ where: { ownerUsername: username }, order: { order: 'ASC' } }); | ||
} | ||
async updateWidgets(username: User['username'], widgets: AvailableWidgets[]) { | ||
const newWidgetOrder = widgets.map((widget, index) => ({ | ||
widget, | ||
order: index, | ||
ownerUsername: username, | ||
})); | ||
await repositories.settings().delete({ ownerUsername: username }); | ||
return repositories.settings().save(newWidgetOrder); | ||
} | ||
} |
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,9 @@ | ||
import Router from '@koa/router'; | ||
import SETTINGS_CONTROLLER from '../controllers/SettingsController'; | ||
|
||
const router = new Router(); | ||
|
||
router.get('/', SETTINGS_CONTROLLER.getUserSettings); | ||
router.put('/', SETTINGS_CONTROLLER.updateSettings); | ||
|
||
export default router.routes(); |
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.