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

Refactor Bolt event handlers to not be HOC's #104

Draft
wants to merge 18 commits into
base: main
Choose a base branch
from
Draft
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
74 changes: 71 additions & 3 deletions app-nest/package-lock.json

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

6 changes: 5 additions & 1 deletion app-nest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
"@nestjs/config": "^3.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.1.8",
"@nestjs/typeorm": "^10.0.0",
"@slack/bolt": "^3.13.2",
"dayjs": "^1.11.9",
"lodash": "^4.17.21",
"pg": "^8.11.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
Expand All @@ -48,6 +51,7 @@
"@nestjs/testing": "^10.0.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/lodash": "^4.14.196",
"@types/node": "^20.3.1",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^5.59.11",
Expand Down Expand Up @@ -81,4 +85,4 @@
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
}
7 changes: 4 additions & 3 deletions app-nest/src/bolt/bolt-register.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ export class BoltRegisterService {
const cref = this.moduleRef.get(discoveredMethod.parentClass.injectType, {
strict: false,
});
// N.B.: Take care where you call the discovered handler method. This is an easy
// place to bind it into a wrong context. Ask me how I know.
return cref[discoveredMethod.methodName]();

return cref[discoveredMethod.methodName].bind(
discoveredMethod.parentClass.instance,
);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions app-nest/src/bolt/enums/bolt-actions.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
enum BoltActions {
SYNC_USERS = "sync_users",
SET_OFFICE_PRESENCE = "set_office_presence",
SET_REMOTE_PRESENCE = "set_remote_presence",
SELECT_OFFICE_FOR_DATE = "select_office_for_date",
DAY_LIST_ITEM_OVERFLOW = "day_list_item_overflow",
SET_VISIBLE_OFFICE = "set_visible_office",
}

export default BoltActions;
6 changes: 6 additions & 0 deletions app-nest/src/bolt/enums/bolt-events.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
enum BoltEvents {
APP_HOME_OPENED = "app_home_opened",
USER_PROFILE_CHANGED = "user_profile_changed",
}

export default BoltEvents;
9 changes: 9 additions & 0 deletions app-nest/src/bolt/types/bolt-action-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {
AllMiddlewareArgs,
SlackAction,
SlackActionMiddlewareArgs,
} from "@slack/bolt";
import { StringIndexed } from "@slack/bolt/dist/types/helpers";

export type BoltActionArgs = SlackActionMiddlewareArgs<SlackAction> &
AllMiddlewareArgs<StringIndexed>;
17 changes: 17 additions & 0 deletions app-nest/src/bolt/types/bolt-event-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
AllMiddlewareArgs,
AppHomeOpenedEvent,
SlackEventMiddlewareArgs,
UserProfileChangedEvent,
} from "@slack/bolt";
import { StringIndexed } from "@slack/bolt/dist/types/helpers";

export type AppHomeOpenedArgs = SlackEventMiddlewareArgs<
AppHomeOpenedEvent["type"]
> &
AllMiddlewareArgs<StringIndexed>;

export type UserProfileChangedArgs = SlackEventMiddlewareArgs<
UserProfileChangedEvent["type"]
> &
AllMiddlewareArgs<StringIndexed>;
12 changes: 6 additions & 6 deletions app-nest/src/dev-tools/dev-tools.controller.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
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 { UserSyncService } from "../sync/user-sync.service";

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

@BoltAction("sync_users")
syncUsers() {
return async ({ ack }) => {
await ack();
await this.userSyncService.syncUsers();
};
@BoltAction(BoltActions.SYNC_USERS)
async syncUsers({ ack }: BoltActionArgs) {
await ack();
await this.userSyncService.syncUsers();
}
}
5 changes: 4 additions & 1 deletion app-nest/src/entities/entities.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Module } from "@nestjs/common";
import { OfficeModule } from "./office/office.module";
import { PresenceModule } from "./presence/presence.module";
import { UserSettingsModule } from "./user-settings/user-settings.module";
import { UserModule } from "./user/user.module";

@Module({
imports: [UserModule],
imports: [UserModule, PresenceModule, OfficeModule, UserSettingsModule],
})
export class EntitiesModule {}
12 changes: 12 additions & 0 deletions app-nest/src/entities/office/office.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Column, Entity, PrimaryGeneratedColumn, Repository } from "typeorm";

@Entity()
export class Office {
@PrimaryGeneratedColumn()
id: number;

@Column()
name: string;
}

export type OfficeRepository = Repository<Office>;
11 changes: 11 additions & 0 deletions app-nest/src/entities/office/office.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { Office } from "./office.entity";
import { OfficeService } from "./office.service";

@Module({
imports: [TypeOrmModule.forFeature([Office])],
providers: [OfficeService],
exports: [TypeOrmModule, OfficeService],
})
export class OfficeModule {}
14 changes: 14 additions & 0 deletions app-nest/src/entities/office/office.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Office, OfficeRepository } from "./office.entity";

@Injectable()
export class OfficeService {
constructor(
@InjectRepository(Office) private officeRepository: OfficeRepository,
) {}

async findAll() {
return this.officeRepository.find();
}
}
8 changes: 8 additions & 0 deletions app-nest/src/entities/presence/dto/presence.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { OmitType, PickType } from "@nestjs/swagger";
import { Presence } from "../presence.entity";

export class UpsertPresenceDto extends OmitType(Presence, ["office"]) {}

export class SetOfficeDto extends PickType(Presence, ["userId", "date"]) {
officeId: number;
}
61 changes: 61 additions & 0 deletions app-nest/src/entities/presence/presence.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
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 { PresenceType } from "./presence.entity";
import { PresenceService } from "./presence.service";

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

@BoltAction(BoltActions.SET_OFFICE_PRESENCE)
async setOfficePresence({ ack, body, payload }: BoltActionArgs) {
await ack();
const date = dayjs(payload["value"]).toDate();
await this.presenceService.upsert({
userId: body.user.id,
type: PresenceType.AT_OFFICE,
date,
});
}

@BoltAction(BoltActions.SET_REMOTE_PRESENCE)
async setRemotePresence({ ack, body, payload }: BoltActionArgs) {
await ack();
const date = dayjs(payload["value"]).toDate();
await this.presenceService.upsert({
userId: body.user.id,
type: PresenceType.REMOTE,
date,
});
}

@BoltAction(BoltActions.SELECT_OFFICE_FOR_DATE)
async selectOfficeForDate({ ack, body, payload }: BoltActionArgs) {
await ack();
const { value, date } = JSON.parse(payload["selected_option"].value);
await this.presenceService.setOffice({
userId: body.user.id,
date: dayjs(date).toDate(),
officeId: value,
});
}

// TODO: Should this be moved?
@BoltAction(BoltActions.DAY_LIST_ITEM_OVERFLOW)
async dayListItemOverflow({ ack, body, payload }: BoltActionArgs) {
await ack();
const { type, date } = JSON.parse(payload["selected_option"].value);

if (type !== "remove_presence") {
throw new InternalServerErrorException("Not implemented.");
}

await this.presenceService.remove({
userId: body.user.id,
date: dayjs(date).toDate(),
});
}
}
24 changes: 24 additions & 0 deletions app-nest/src/entities/presence/presence.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Column, Entity, ManyToOne, PrimaryColumn, Repository } from "typeorm";
import { Office } from "../office/office.entity";

export enum PresenceType {
AT_OFFICE = "at_office",
REMOTE = "remote",
}

@Entity()
export class Presence {
@PrimaryColumn()
userId: string;

@PrimaryColumn({ type: "date" })
date: Date;

@Column({ type: "enum", enum: PresenceType, nullable: true })
type: PresenceType | null;

@ManyToOne(() => Office, { nullable: true })
office: Office;
}

export type PresenceRepository = Repository<Presence>;
Loading