Skip to content

Commit

Permalink
Merge pull request #27 from funidata/presence-model-rework
Browse files Browse the repository at this point in the history
Presence model rework
  • Loading branch information
joonashak authored Jan 8, 2025
2 parents 8f24382 + 7ce91dd commit 50da485
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { MigrationInterface, QueryRunner, Table } from "typeorm";
import { PresenceType } from "../../entities/presence/presence.model";

// Moved here after the (imported) enum was removed from code.
enum PresenceType {
OFFICE = "office",
REMOTE = "remote",
}

export class CreatePresenceTable1726035621508 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { MigrationInterface, QueryRunner, TableColumn } from "typeorm";

export class ReplacePresenceTypeWithRemoteColumn1735633836694 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
/*
* N.B.: Values are not migrated from `type` to `remote` because Kaiku is
* not running anywhere yet and the default value of `remote`, while
* sometimes incorrect, will not break anything.
*/
const table = await queryRunner.getTable("presence");

await queryRunner.dropColumn(table, "type");

await queryRunner.addColumn(
table,
new TableColumn({ name: "remote", type: "boolean", isNullable: false, default: false }),
);
}

public async down(): Promise<void> {}
}
4 changes: 1 addition & 3 deletions app/src/entities/presence/dto/presence-filter.dto.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { PresenceType } from "../presence.model";

export type PresenceFilter = {
date?: string;
startDate?: string;
endDate?: string;
officeId?: string;
type?: PresenceType;
remote?: boolean;
userGroup?: string;
};
3 changes: 1 addition & 2 deletions app/src/entities/presence/dto/presence.dto.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { PickType } from "@nestjs/swagger";
import { PresenceType } from "../presence.model";

export class UpsertPresenceDto {
userId: string;
date: string;
type?: PresenceType;
remote?: boolean;
officeId?: string;
}

Expand Down
4 changes: 1 addition & 3 deletions app/src/entities/presence/presence.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Action from "../../bolt/enums/action.enum";
import { BoltActionArgs } from "../../bolt/types/bolt-action-args.type";
import { HomeTabService } from "../../gui/home-tab/home-tab.service";
import { RegistrationView } from "../../gui/home-tab/views/registration/registration.view";
import { PresenceType } from "./presence.model";
import { PresenceService } from "./presence.service";

@Controller()
Expand All @@ -20,7 +19,6 @@ export class PresenceController {
const date = args.payload["value"];
await this.presenceService.upsert({
userId: args.body.user.id,
type: PresenceType.OFFICE,
date,
});

Expand All @@ -32,7 +30,7 @@ export class PresenceController {
const date = args.payload["value"];
await this.presenceService.upsert({
userId: args.body.user.id,
type: PresenceType.REMOTE,
remote: true,
date,
});

Expand Down
16 changes: 9 additions & 7 deletions app/src/entities/presence/presence.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ import { Column, Entity, JoinColumn, ManyToOne, PrimaryColumn, Repository } from
import { Office } from "../office/office.model";
import { User } from "../user/user.model";

export enum PresenceType {
OFFICE = "office",
REMOTE = "remote",
}

@Entity()
export class Presence {
@ManyToOne(() => User, { nullable: false, eager: true })
Expand All @@ -20,8 +15,15 @@ export class Presence {
@PrimaryColumn({ type: "date" })
date: string;

@Column({ type: "enum", enum: PresenceType, nullable: true })
type: PresenceType | null;
/**
* Indicates whether user is working remotely or at office.
*
* This field should be always considered when handling presences – if there
* are no offices added to Kaiku, the `office` field will always be empty
* rendering using it alone for logic unreliable.
*/
@Column({ name: "remote", type: "boolean", nullable: false, default: false })
remote: boolean;

@ManyToOne(() => Office, { nullable: true, eager: true })
@JoinColumn({ name: "office_id" })
Expand Down
6 changes: 3 additions & 3 deletions app/src/gui/home-tab/views/presence/presence.view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import dayjs, { Dayjs } from "dayjs";
import { Divider, Header } from "slack-block-builder";
import { Appendable, ViewBlockBuilder } from "slack-block-builder/dist/internal";
import { workDayRange } from "../../../../common/work-day-range";
import { Presence, PresenceType } from "../../../../entities/presence/presence.model";
import { Presence } from "../../../../entities/presence/presence.model";
import { PresenceService } from "../../../../entities/presence/presence.service";
import { UserSettings } from "../../../../entities/user-settings/user-settings.model";
import { UserSettingsService } from "../../../../entities/user-settings/user-settings.service";
Expand Down Expand Up @@ -46,15 +46,15 @@ export class PresenceView {
const startDate = settings.dateFilter || new Date().toISOString();
const endDate = dayjs(startDate).add(2, "weeks").toISOString();

const type = settings.officeFilter === "REMOTE" ? PresenceType.REMOTE : PresenceType.OFFICE;
const remote = settings.officeFilter === "REMOTE";
const officeId = !["REMOTE", "ALL_OFFICES"].includes(settings.officeFilter)
? settings.officeFilter
: null;

const entries = await this.presenceService.findByFilter({
startDate,
endDate,
type,
remote,
officeId,
userGroup: settings.userGroupFilter,
});
Expand Down
4 changes: 2 additions & 2 deletions app/src/gui/home-tab/views/registration/day-list-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export class DayListItem {
text: "Toimistolla",
actionId: Action.SET_OFFICE_PRESENCE,
value: dateString,
}).primary(currentPresence?.type === "office"),
}).primary(currentPresence?.remote === false),
Button({
text: "Etänä",
actionId: Action.SET_REMOTE_PRESENCE,
value: dateString,
}).primary(currentPresence?.type === "remote"),
}).primary(currentPresence?.remote === true),
this.getOfficeBlocks(props),
OverflowMenu({ actionId: Action.DAY_LIST_ITEM_OVERFLOW }).options(
Option({
Expand Down

0 comments on commit 50da485

Please sign in to comment.