Skip to content

Commit

Permalink
Persist user's visible office setting
Browse files Browse the repository at this point in the history
  • Loading branch information
joonashak committed Aug 11, 2023
1 parent 02b0c06 commit eda050c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions app-nest/src/bolt/enums/bolt-actions.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ enum BoltActions {
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;
20 changes: 20 additions & 0 deletions app-nest/src/entities/user-settings/user-settings.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
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 { UserSettingsService } from "./user-settings.service";

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

@BoltAction(BoltActions.SET_VISIBLE_OFFICE)
async setVisibleOffice({ ack, payload, body }: BoltActionArgs) {
await ack();
const visibleOfficeId = Number(payload["selected_option"].value);
await this.userSettingsService.setVisibleOffice({
userSlackId: body.user.id,
visibleOfficeId,
});
}
}
13 changes: 13 additions & 0 deletions app-nest/src/entities/user-settings/user-settings.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { OmitType, PickType } from "@nestjs/swagger";
import { UserSettings } from "./user-settings.entity";

export class UpsertUserSettingsDto extends OmitType(UserSettings, [
"visibleOffice",
"user",
]) {}

export class SetVisibleOfficeDto extends PickType(UserSettings, [
"userSlackId",
]) {
visibleOfficeId: number;
}
2 changes: 2 additions & 0 deletions app-nest/src/entities/user-settings/user-settings.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { UserSettingsController } from "./user-settings.controller";
import { UserSettings } from "./user-settings.entity";
import { UserSettingsService } from "./user-settings.service";

@Module({
imports: [TypeOrmModule.forFeature([UserSettings])],
providers: [UserSettingsService],
controllers: [UserSettingsController],
exports: [TypeOrmModule, UserSettingsService],
})
export class UserSettingsModule {}
19 changes: 19 additions & 0 deletions app-nest/src/entities/user-settings/user-settings.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import {
SetVisibleOfficeDto,
UpsertUserSettingsDto,
} from "./user-settings.dto";
import { UserSettings, UserSettingsRepository } from "./user-settings.entity";

@Injectable()
Expand All @@ -8,4 +12,19 @@ export class UserSettingsService {
@InjectRepository(UserSettings)
private userSettingsRepository: UserSettingsRepository,
) {}

async upsert(input: UpsertUserSettingsDto) {
return await this.userSettingsRepository.upsert(input, ["userSlackId"]);
}

async setVisibleOffice({
userSlackId,
visibleOfficeId,
}: SetVisibleOfficeDto) {
await this.upsert({ userSlackId });
return this.userSettingsRepository.update(
{ userSlackId },
{ visibleOffice: { id: visibleOfficeId } },
);
}
}
3 changes: 2 additions & 1 deletion app-nest/src/gui/tabs/home/visible-office-select.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
StaticSelect,
ViewBlockBuilder,
} from "slack-block-builder";
import BoltActions from "../../../bolt/enums/bolt-actions.enum";
import { OfficeService } from "../../../entities/office/office.service";
import { BlockBuilder } from "../../block-builder.interface";

Expand All @@ -29,7 +30,7 @@ export class VisibleOfficeSelectBuilder
}).accessory(
StaticSelect({
placeholder: "Valitse toimipiste",
actionId: "asd",
actionId: BoltActions.SET_VISIBLE_OFFICE,
})
// TODO: Use user's selected office as initial value.
.initialOption(Options[0])
Expand Down

0 comments on commit eda050c

Please sign in to comment.