Skip to content

Commit

Permalink
Use office relation in presence model
Browse files Browse the repository at this point in the history
  • Loading branch information
joonashak committed Aug 11, 2023
1 parent bc08253 commit 55c75a2
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 12 deletions.
60 changes: 57 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.

1 change: 1 addition & 0 deletions app-nest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@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",
Expand Down
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;
}
4 changes: 2 additions & 2 deletions app-nest/src/entities/presence/presence.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ export class PresenceController {
async selectOfficeForDate({ ack, body, payload }: BoltActionArgs) {
await ack();
const { value, date } = JSON.parse(payload["selected_option"].value);
await this.presenceService.upsert({
await this.presenceService.setOffice({
userId: body.user.id,
date: dayjs(date).toDate(),
office: value,
officeId: value,
});
}

Expand Down
7 changes: 4 additions & 3 deletions app-nest/src/entities/presence/presence.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Column, Entity, PrimaryColumn, Repository } from "typeorm";
import { Column, Entity, ManyToOne, PrimaryColumn, Repository } from "typeorm";
import { Office } from "../office/office.entity";

export enum PresenceType {
AT_OFFICE = "at_office",
Expand All @@ -16,8 +17,8 @@ export class Presence {
@Column({ type: "enum", enum: PresenceType, nullable: true })
type: PresenceType | null;

@Column({ nullable: true })
office: string | null;
@ManyToOne(() => Office, { nullable: true })
office: Office;
}

export type PresenceRepository = Repository<Presence>;
3 changes: 2 additions & 1 deletion app-nest/src/entities/presence/presence.module.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
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.entity";
import { PresenceService } from "./presence.service";

@Module({
imports: [TypeOrmModule.forFeature([Presence])],
imports: [TypeOrmModule.forFeature([Presence]), OfficeModule],
providers: [PresenceService],
controllers: [PresenceController],
exports: [TypeOrmModule],
Expand Down
22 changes: 19 additions & 3 deletions app-nest/src/entities/presence/presence.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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.entity";

@Injectable()
Expand All @@ -10,14 +11,22 @@ export class PresenceService {
private dataSource: DataSource,
) {}

async upsert(presence: Partial<Presence>) {
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()
Expand All @@ -27,7 +36,14 @@ export class PresenceService {
.execute();
}

async remove(presence: Pick<Presence, "userId" | "date">) {
return this.presenceRepository.delete(presence);
async setOffice({ userId, date, officeId }: SetOfficeDto) {
await this.upsert({ userId, date });

return this.presenceRepository.update(
{ userId, date },
{
office: { id: officeId },
},
);
}
}

0 comments on commit 55c75a2

Please sign in to comment.