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

Miscellaneous updates #15

Merged
merged 3 commits into from
Sep 18, 2024
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
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
38 changes: 23 additions & 15 deletions app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 20 additions & 4 deletions app/src/bolt/bolt.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from "@nestjs/common";
import { Injectable, Logger } from "@nestjs/common";
import { App, LogLevel } from "@slack/bolt";
import { StringIndexed } from "@slack/bolt/dist/types/helpers";
import { ConfigService } from "../common/config/config.service";
Expand All @@ -7,23 +7,24 @@ import { BoltLogger } from "./bolt-logger";
@Injectable()
export class BoltService {
private bolt: App<StringIndexed>;
private logger = new Logger(BoltService.name);

constructor(private configService: ConfigService) {}

async connect() {
const { appToken, token, signingSecret } = this.configService.getConfig().bolt;
const logger = new BoltLogger();
const boltLogger = new BoltLogger();

this.bolt = new App({
appToken,
token,
signingSecret,
socketMode: true,
logger,
logger: boltLogger,
logLevel: LogLevel.INFO,
});

await this.bolt.start();
await this.connectWithRetry();
}

async disconnect() {
Expand All @@ -33,4 +34,19 @@ export class BoltService {
getBolt(): App<StringIndexed> {
return this.bolt;
}

private async connectWithRetry(): Promise<void> {
// eslint-disable-next-line no-constant-condition
while (true) {
try {
await this.bolt.start();
return;
} catch (error) {
this.logger.error("Failed to initialize a connection to Slack.", error);
this.logger.log("Trying to connect again to Slack.");

await new Promise((resolve) => setTimeout(resolve, 2000));
}
}
}
}
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>;
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
Loading
Loading