-
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.
Save constant presence selection in database
- Loading branch information
Showing
8 changed files
with
128 additions
and
13 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
enum ViewAction { | ||
CREATE_OFFICE = "create_office", | ||
EDIT_OFFICE = "edit_office", | ||
SAVE_CONSTANT_PRESENCES = "save_constant_presences", | ||
} | ||
|
||
export default ViewAction; |
7 changes: 6 additions & 1 deletion
7
app/src/entities/constant-presence/constant-presence.module.ts
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 |
---|---|---|
@@ -1,6 +1,11 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { TypeOrmModule } from "@nestjs/typeorm"; | ||
import { ConstantPresence } from "./constant-presence.model"; | ||
import { ConstantPresenceService } from "./constant-presence.service"; | ||
|
||
@Module({ imports: [TypeOrmModule.forFeature([ConstantPresence])], exports: [TypeOrmModule] }) | ||
@Module({ | ||
imports: [TypeOrmModule.forFeature([ConstantPresence])], | ||
providers: [ConstantPresenceService], | ||
exports: [TypeOrmModule, ConstantPresenceService], | ||
}) | ||
export class ConstantPresenceModule {} |
54 changes: 54 additions & 0 deletions
54
app/src/entities/constant-presence/constant-presence.service.ts
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,54 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { InjectRepository } from "@nestjs/typeorm"; | ||
import dayjs from "../../common/dayjs"; | ||
import { ConstantPresence, ConstantPresenceRepository } from "./constant-presence.model"; | ||
import { CreateConstantPresence } from "./dto/create-constant-presence.dto"; | ||
|
||
@Injectable() | ||
export class ConstantPresenceService { | ||
constructor( | ||
@InjectRepository(ConstantPresence) | ||
private constantPresenceRepository: ConstantPresenceRepository, | ||
) {} | ||
|
||
// async findEffectiveByUserId(userId: string) { | ||
// return this.constantPresenceRepository | ||
// .createQueryBuilder("cp") | ||
// .select() | ||
// .where("cp.user_slack_id = :userId", { userId }) | ||
// .andWhere("in_effect @> NOW()::date") | ||
// .execute(); | ||
// } | ||
|
||
/** | ||
* Create new constant presences for user. | ||
* | ||
* All currently effective constant presence entries will be closed as of the | ||
* current date. New entries will be effective starting on the current date. | ||
*/ | ||
async closeOldAndCreateMany(userId: string, cps: CreateConstantPresence[]) { | ||
await this.closeEffectiveCpsByUserId(userId); | ||
|
||
const user = { slackId: userId }; | ||
const inEffect = { start: dayjs(), end: undefined }; | ||
|
||
const inserts = cps.map(({ officeId, ...cp }) => ({ | ||
...cp, | ||
user, | ||
inEffect, | ||
office: officeId ? { id: officeId } : null, | ||
})); | ||
|
||
await this.constantPresenceRepository.save(inserts); | ||
} | ||
|
||
private async closeEffectiveCpsByUserId(userId: string) { | ||
return this.constantPresenceRepository | ||
.createQueryBuilder() | ||
.update() | ||
.set({ inEffect: () => "daterange(lower(in_effect), NOW()::date)" }) | ||
.where("user_slack_id = :userId", { userId }) | ||
.andWhere("in_effect @> NOW()::date") | ||
.execute(); | ||
} | ||
} |
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
5 changes: 5 additions & 0 deletions
5
app/src/entities/constant-presence/dto/create-constant-presence.dto.ts
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,5 @@ | ||
import { ConstantPresence } from "../constant-presence.model"; | ||
|
||
export type CreateConstantPresence = Pick<ConstantPresence, "dayOfWeek" | "remote"> & { | ||
officeId: string | undefined; | ||
}; |
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
3 changes: 2 additions & 1 deletion
3
...me-tab/views/settings/constant-presence-management/constant-presence-management.module.ts
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