Skip to content

Commit

Permalink
Select office by day through UI
Browse files Browse the repository at this point in the history
  • Loading branch information
joonashak committed Aug 9, 2023
1 parent 45801f4 commit 5c7d0dc
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 8 deletions.
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
@@ -1,6 +1,7 @@
enum BoltActions {
SYNC_USERS = "sync_users",
REGISTER_PRESENCE = "register_presence",
SELECT_OFFICE_FOR_DATE = "select_office_for_date",
}

export default BoltActions;
11 changes: 11 additions & 0 deletions app-nest/src/entities/presence/presence.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,15 @@ export class PresenceController {
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.upsert({
userId: body.user.id,
date: dayjs(date).toDate(),
office: value,
});
}
}
7 changes: 5 additions & 2 deletions app-nest/src/entities/presence/presence.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ export class Presence {
@PrimaryColumn({ type: "date" })
date: Date;

@Column({ type: "enum", enum: PresenceType })
type: PresenceType;
@Column({ type: "enum", enum: PresenceType, nullable: true })
type: PresenceType | null;

@Column({ nullable: true })
office: string | null;
}

export type PresenceRepository = Repository<Presence>;
11 changes: 9 additions & 2 deletions app-nest/src/entities/presence/presence.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ export class PresenceService {
private dataSource: DataSource,
) {}

async upsert(presence: Presence) {
async upsert(presence: Partial<Presence>) {
// 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),
);

return this.dataSource
.createQueryBuilder()
.insert()
.into(Presence)
.values(presence)
.orUpdate(["type"], ["userId", "date"])
.orUpdate(updatableCols, primaryKeys)
.execute();
}
}
8 changes: 4 additions & 4 deletions app-nest/src/gui/tabs/home/day-list-item.blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const getDayListItemBlocks = ({ date }: DayListItemProps) => [
},
},
{
action_id: "text1234",
action_id: BoltActions.SELECT_OFFICE_FOR_DATE,
type: "static_select",
placeholder: {
type: "plain_text",
Expand All @@ -45,22 +45,22 @@ const getDayListItemBlocks = ({ date }: DayListItemProps) => [
type: "plain_text",
text: "Helsinki",
},
value: "hki",
value: JSON.stringify({ value: "hki", date: date.toISOString() }),
},
options: [
{
text: {
type: "plain_text",
text: "Helsinki",
},
value: "hki",
value: JSON.stringify({ value: "hki", date: date.toISOString() }),
},
{
text: {
type: "plain_text",
text: "Tampere",
},
value: "tre",
value: JSON.stringify({ value: "tre", date: date.toISOString() }),
},
],
},
Expand Down

0 comments on commit 5c7d0dc

Please sign in to comment.