Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
joonashak committed Sep 18, 2024
1 parent 628f7de commit 232e870
Show file tree
Hide file tree
Showing 19 changed files with 56 additions and 58 deletions.
1 change: 1 addition & 0 deletions app/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const config: Config = {
".module.ts$",
".model.ts$",
".dto.ts$",
".type.ts$",
"src/database/migrations",
"src/database/migration-config.ts",
"src/main.ts",
Expand Down
10 changes: 5 additions & 5 deletions app/src/bolt/decorators/bolt-action.decorator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import BoltActions from "../enums/bolt-actions.enum";
import Action from "../enums/action.enum";
import BoltAction, { BOLT_ACTION_KEY } from "./bolt-action.decorator";

describe("@BoltAction", () => {
class TestController {
@BoltAction(BoltActions.SYNC_USERS)
@BoltAction(Action.SYNC_USERS)
public static syncUsers() {}

@BoltAction(BoltActions.SET_OFFICE_PRESENCE)
@BoltAction(Action.SET_OFFICE_PRESENCE)
public static setOfficePresence() {}
}

it("Assigns correct metadata to decorated class", () => {
const metaValueForSync = Reflect.getMetadata(BOLT_ACTION_KEY, TestController.syncUsers);
expect(metaValueForSync).toEqual(BoltActions.SYNC_USERS);
expect(metaValueForSync).toEqual(Action.SYNC_USERS);

const metaValueForSet = Reflect.getMetadata(BOLT_ACTION_KEY, TestController.setOfficePresence);
expect(metaValueForSet).toEqual(BoltActions.SET_OFFICE_PRESENCE);
expect(metaValueForSet).toEqual(Action.SET_OFFICE_PRESENCE);
});
});
4 changes: 2 additions & 2 deletions app/src/bolt/decorators/bolt-action.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { SetMetadata } from "@nestjs/common";
import BoltActions from "../enums/bolt-actions.enum";
import Action from "../enums/action.enum";

export const BOLT_ACTION_KEY = "BoltAction";

const BoltAction = (actionName: BoltActions) => SetMetadata(BOLT_ACTION_KEY, actionName);
const BoltAction = (actionName: Action) => SetMetadata(BOLT_ACTION_KEY, actionName);

export default BoltAction;
10 changes: 5 additions & 5 deletions app/src/bolt/decorators/bolt-event.decorator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import BoltEvents from "../enums/bolt-events.enum";
import Event from "../enums/event.enum";
import BoltEvent, { BOLT_EVENT_KEY } from "./bolt-event.decorator";

describe("@BoltEvent", () => {
class TestController {
@BoltEvent(BoltEvents.APP_HOME_OPENED)
@BoltEvent(Event.APP_HOME_OPENED)
public static appHomeOpened() {}

@BoltEvent(BoltEvents.USER_PROFILE_CHANGED)
@BoltEvent(Event.USER_PROFILE_CHANGED)
public static userProfileChanged() {}
}

it("Assigns correct metadata to decorated class", () => {
const metaValueForSync = Reflect.getMetadata(BOLT_EVENT_KEY, TestController.appHomeOpened);
expect(metaValueForSync).toEqual(BoltEvents.APP_HOME_OPENED);
expect(metaValueForSync).toEqual(Event.APP_HOME_OPENED);

const metaValueForSet = Reflect.getMetadata(BOLT_EVENT_KEY, TestController.userProfileChanged);
expect(metaValueForSet).toEqual(BoltEvents.USER_PROFILE_CHANGED);
expect(metaValueForSet).toEqual(Event.USER_PROFILE_CHANGED);
});
});
4 changes: 2 additions & 2 deletions app/src/bolt/decorators/bolt-event.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { SetMetadata } from "@nestjs/common";
import BoltEvents from "../enums/bolt-events.enum";
import Event from "../enums/event.enum";

export const BOLT_EVENT_KEY = "BoltEvent";

const BoltEvent = (eventName: BoltEvents) => SetMetadata(BOLT_EVENT_KEY, eventName);
const BoltEvent = (eventName: Event) => SetMetadata(BOLT_EVENT_KEY, eventName);

export default BoltEvent;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
enum BoltActions {
enum Action {
SYNC_USERS = "sync_users",
SET_OFFICE_PRESENCE = "set_office_presence",
SET_REMOTE_PRESENCE = "set_remote_presence",
Expand All @@ -7,4 +7,4 @@ enum BoltActions {
SET_VISIBLE_OFFICE = "set_visible_office",
}

export default BoltActions;
export default Action;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
enum BoltEvents {
enum Event {
APP_HOME_OPENED = "app_home_opened",
USER_PROFILE_CHANGED = "user_profile_changed",
}

export default BoltEvents;
export default Event;
5 changes: 5 additions & 0 deletions app/src/bolt/types/app-home-opened.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { AllMiddlewareArgs, AppHomeOpenedEvent, SlackEventMiddlewareArgs } from "@slack/bolt";
import { StringIndexed } from "@slack/bolt/dist/types/helpers";

export type AppHomeOpenedArgs = SlackEventMiddlewareArgs<AppHomeOpenedEvent["type"]> &
AllMiddlewareArgs<StringIndexed>;
File renamed without changes.
13 changes: 0 additions & 13 deletions app/src/bolt/types/bolt-event-types.ts

This file was deleted.

5 changes: 5 additions & 0 deletions app/src/bolt/types/user-profile-changed.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { AllMiddlewareArgs, SlackEventMiddlewareArgs, UserProfileChangedEvent } from "@slack/bolt";
import { StringIndexed } from "@slack/bolt/dist/types/helpers";

export type UserProfileChangedArgs = SlackEventMiddlewareArgs<UserProfileChangedEvent["type"]> &
AllMiddlewareArgs<StringIndexed>;
6 changes: 3 additions & 3 deletions app/src/dev-tools/dev-tools.controller.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Controller } from "@nestjs/common";
import BoltAction from "../bolt/decorators/bolt-action.decorator";
import BoltActions from "../bolt/enums/bolt-actions.enum";
import { BoltActionArgs } from "../bolt/types/bolt-action-types";
import Action from "../bolt/enums/action.enum";
import { BoltActionArgs } from "../bolt/types/bolt-action-args.type";
import { UserSyncService } from "../sync/user-sync.service";

@Controller()
export class DevToolsController {
constructor(private userSyncService: UserSyncService) {}

@BoltAction(BoltActions.SYNC_USERS)
@BoltAction(Action.SYNC_USERS)
async syncUsers({ ack }: BoltActionArgs) {
await ack();
await this.userSyncService.syncUsers();
Expand Down
12 changes: 6 additions & 6 deletions app/src/entities/presence/presence.controller.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Controller, InternalServerErrorException } from "@nestjs/common";
import dayjs from "dayjs";
import BoltAction from "../../bolt/decorators/bolt-action.decorator";
import BoltActions from "../../bolt/enums/bolt-actions.enum";
import { BoltActionArgs } from "../../bolt/types/bolt-action-types";
import Action from "../../bolt/enums/action.enum";
import { BoltActionArgs } from "../../bolt/types/bolt-action-args.type";
import { PresenceType } from "./presence.model";
import { PresenceService } from "./presence.service";

@Controller()
export class PresenceController {
constructor(private presenceService: PresenceService) {}

@BoltAction(BoltActions.SET_OFFICE_PRESENCE)
@BoltAction(Action.SET_OFFICE_PRESENCE)
async setOfficePresence({ ack, body, payload }: BoltActionArgs) {
await ack();
const date = dayjs(payload["value"]).toDate();
Expand All @@ -21,7 +21,7 @@ export class PresenceController {
});
}

@BoltAction(BoltActions.SET_REMOTE_PRESENCE)
@BoltAction(Action.SET_REMOTE_PRESENCE)
async setRemotePresence({ ack, body, payload }: BoltActionArgs) {
await ack();
const date = dayjs(payload["value"]).toDate();
Expand All @@ -32,7 +32,7 @@ export class PresenceController {
});
}

@BoltAction(BoltActions.SELECT_OFFICE_FOR_DATE)
@BoltAction(Action.SELECT_OFFICE_FOR_DATE)
async selectOfficeForDate({ ack, body, payload }: BoltActionArgs) {
await ack();
const { value, date } = JSON.parse(payload["selected_option"].value);
Expand All @@ -44,7 +44,7 @@ export class PresenceController {
}

// TODO: Should this be moved?
@BoltAction(BoltActions.DAY_LIST_ITEM_OVERFLOW)
@BoltAction(Action.DAY_LIST_ITEM_OVERFLOW)
async dayListItemOverflow({ ack, body, payload }: BoltActionArgs) {
await ack();
const { type, date } = JSON.parse(payload["selected_option"].value);
Expand Down
6 changes: 3 additions & 3 deletions app/src/entities/user-settings/user-settings.controller.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Controller } from "@nestjs/common";
import BoltAction from "../../bolt/decorators/bolt-action.decorator";
import BoltActions from "../../bolt/enums/bolt-actions.enum";
import { BoltActionArgs } from "../../bolt/types/bolt-action-types";
import Action from "../../bolt/enums/action.enum";
import { BoltActionArgs } from "../../bolt/types/bolt-action-args.type";
import { UserSettingsService } from "./user-settings.service";

@Controller()
export class UserSettingsController {
constructor(private userSettingsService: UserSettingsService) {}

@BoltAction(BoltActions.SET_VISIBLE_OFFICE)
@BoltAction(Action.SET_VISIBLE_OFFICE)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async setVisibleOffice({ ack, payload, body }: BoltActionArgs) {
await ack();
Expand Down
4 changes: 2 additions & 2 deletions app/src/gui/dev/dev-ui.builder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from "@nestjs/common";
import { Actions, Button, Context, Divider, Header, ViewBlockBuilder } from "slack-block-builder";
import BoltActions from "../../bolt/enums/bolt-actions.enum";
import Action from "../../bolt/enums/action.enum";
import { BlockBuilder } from "../block-builder.interface";

@Injectable()
Expand All @@ -11,7 +11,7 @@ export class DevUiBuilder implements BlockBuilder<ViewBlockBuilder> {
Actions().elements(
Button({
text: ":recycle: Sync Users",
actionId: BoltActions.SYNC_USERS,
actionId: Action.SYNC_USERS,
}),
),
Context().elements(
Expand Down
10 changes: 5 additions & 5 deletions app/src/gui/tabs/home/day-list-item.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
StaticSelect,
ViewBlockBuilder,
} from "slack-block-builder";
import BoltActions from "../../../bolt/enums/bolt-actions.enum";
import Action from "../../../bolt/enums/action.enum";
import { Office } from "../../../entities/office/office.model";
import { BlockBuilder } from "../../block-builder.interface";

Expand All @@ -29,16 +29,16 @@ export class DayListItemBuilder implements BlockBuilder<ViewBlockBuilder> {
Actions().elements(
Button({
text: "Toimistolla",
actionId: BoltActions.SET_OFFICE_PRESENCE,
actionId: Action.SET_OFFICE_PRESENCE,
value: dateString,
}),
Button({
text: "Etänä",
actionId: BoltActions.SET_REMOTE_PRESENCE,
actionId: Action.SET_REMOTE_PRESENCE,
value: dateString,
}),
this.getOfficeBlocks(props),
OverflowMenu({ actionId: BoltActions.DAY_LIST_ITEM_OVERFLOW }).options(
OverflowMenu({ actionId: Action.DAY_LIST_ITEM_OVERFLOW }).options(
Option({
text: "Poista ilmoittautuminen",
value: JSON.stringify({
Expand Down Expand Up @@ -66,7 +66,7 @@ export class DayListItemBuilder implements BlockBuilder<ViewBlockBuilder> {

return StaticSelect({
placeholder: "Valitse toimipiste",
actionId: BoltActions.SELECT_OFFICE_FOR_DATE,
actionId: Action.SELECT_OFFICE_FOR_DATE,
})
.initialOption(Options[0])
.options(Options);
Expand Down
6 changes: 3 additions & 3 deletions app/src/gui/tabs/home/home-tab.controller.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Controller } from "@nestjs/common";
import { HomeTab } from "slack-block-builder";
import BoltEvent from "../../../bolt/decorators/bolt-event.decorator";
import BoltEvents from "../../../bolt/enums/bolt-events.enum";
import { AppHomeOpenedArgs } from "../../../bolt/types/bolt-event-types";
import Event from "../../../bolt/enums/event.enum";
import { AppHomeOpenedArgs } from "../../../bolt/types/app-home-opened.type";
import { HomeTabBuilder } from "./home-tab.builder";

@Controller()
export class HomeTabController {
constructor(private homeTabBlocks: HomeTabBuilder) {}

@BoltEvent(BoltEvents.APP_HOME_OPENED)
@BoltEvent(Event.APP_HOME_OPENED)
async getView({ event, client, logger }: AppHomeOpenedArgs) {
const blocks = await this.homeTabBlocks.build();
const view = HomeTab()
Expand Down
4 changes: 2 additions & 2 deletions app/src/gui/tabs/home/visible-office-select.builder.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from "@nestjs/common";
import { Option, Section, StaticSelect, ViewBlockBuilder } from "slack-block-builder";
import BoltActions from "../../../bolt/enums/bolt-actions.enum";
import Action from "../../../bolt/enums/action.enum";
import { OfficeService } from "../../../entities/office/office.service";
import { BlockBuilder } from "../../block-builder.interface";

Expand Down Expand Up @@ -29,7 +29,7 @@ export class VisibleOfficeSelectBuilder implements BlockBuilder<ViewBlockBuilder
}).accessory(
StaticSelect({
placeholder: "Valitse toimipiste",
actionId: BoltActions.SET_VISIBLE_OFFICE,
actionId: Action.SET_VISIBLE_OFFICE,
})
// TODO: Use user's selected office as initial value.
.initialOption(Options[0])
Expand Down
6 changes: 3 additions & 3 deletions app/src/sync/sync.controller.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Controller } from "@nestjs/common";
import BoltEvent from "../bolt/decorators/bolt-event.decorator";
import BoltEvents from "../bolt/enums/bolt-events.enum";
import { UserProfileChangedArgs } from "../bolt/types/bolt-event-types";
import Event from "../bolt/enums/event.enum";
import { UserProfileChangedArgs } from "../bolt/types/user-profile-changed.type";
import { UserSyncService } from "./user-sync.service";

@Controller()
export class SyncController {
constructor(private userSyncService: UserSyncService) {}

@BoltEvent(BoltEvents.USER_PROFILE_CHANGED)
@BoltEvent(Event.USER_PROFILE_CHANGED)
async userProfileChanged({ event }: UserProfileChangedArgs) {
await this.userSyncService.syncUsers(event.user);
}
Expand Down

0 comments on commit 232e870

Please sign in to comment.