-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Copy over presence module from Hybridilusmu
- Loading branch information
Showing
6 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { OmitType, PickType } from "@nestjs/swagger"; | ||
import { Presence } from "../presence.model"; | ||
|
||
export class UpsertPresenceDto extends OmitType(Presence, ["office"]) {} | ||
|
||
export class SetOfficeDto extends PickType(Presence, ["userId", "date"]) { | ||
officeId: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.model"; | ||
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(), | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.model"; | ||
|
||
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { TypeOrmModule } from "@nestjs/typeorm"; | ||
import { OfficeModule } from "../office/office.module"; | ||
import { PresenceController } from "./presence.controller"; | ||
import { Presence } from "./presence.model"; | ||
import { PresenceService } from "./presence.service"; | ||
|
||
@Module({ | ||
imports: [TypeOrmModule.forFeature([Presence]), OfficeModule], | ||
providers: [PresenceService], | ||
controllers: [PresenceController], | ||
exports: [TypeOrmModule], | ||
}) | ||
export class PresenceModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { InjectRepository } from "@nestjs/typeorm"; | ||
import { DataSource } from "typeorm"; | ||
import { SetOfficeDto, UpsertPresenceDto } from "./dto/presence.dto"; | ||
import { Presence, PresenceRepository } from "./presence.model"; | ||
|
||
@Injectable() | ||
export class PresenceService { | ||
constructor( | ||
@InjectRepository(Presence) private presenceRepository: PresenceRepository, | ||
private dataSource: DataSource, | ||
) {} | ||
|
||
async remove(presence: Pick<Presence, "userId" | "date">) { | ||
return this.presenceRepository.delete(presence); | ||
} | ||
|
||
async upsert(presence: Partial<UpsertPresenceDto>) { | ||
// Select only existing cols for the upsert operation to avoid overriding | ||
// existing data with defaults/nulls. | ||
const primaryKeys = ["userId", "date"]; | ||
const updatableCols = Object.keys(presence).filter((key) => !primaryKeys.includes(key)); | ||
|
||
if (updatableCols.length === 0) { | ||
return; | ||
} | ||
|
||
return this.dataSource | ||
.createQueryBuilder() | ||
.insert() | ||
.into(Presence) | ||
.values(presence) | ||
.orUpdate(updatableCols, primaryKeys) | ||
.execute(); | ||
} | ||
|
||
async setOffice({ userId, date, officeId }: SetOfficeDto) { | ||
await this.upsert({ userId, date }); | ||
|
||
return this.presenceRepository.update( | ||
{ userId, date }, | ||
{ | ||
office: { id: officeId }, | ||
}, | ||
); | ||
} | ||
} |