Skip to content

Commit

Permalink
Add "autoGenerated" working-hours-entry field
Browse files Browse the repository at this point in the history
  • Loading branch information
ba1uev committed Oct 29, 2024
1 parent 0da094a commit 6d82369
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 25 deletions.
48 changes: 24 additions & 24 deletions src/integrations/matrix/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,6 @@ class Matrix extends Integration {
.then((res) => Object.keys(res.data?.joined || {}))
}

private async sendMessageInRoom(
roomId: MatrixRoomId,
message: string
): Promise<void> {
if (config.debug) {
console.log(`Matrix notification skipped (debug mode): ${message}`)
return
}
return axios
.post(
`https://${this.credentials.host}/_matrix/client/r0/rooms/${roomId}/send/m.room.message?access_token=${this.credentials.token}`,
{
msgtype: 'm.text',
format: 'org.matrix.custom.html',
body: message.replace(this.tagsRe, ''),
formatted_body: message,
},
{
headers: this.headers,
}
)
.then(() => {})
}

private async ensureUserRoom(user: User): Promise<MatrixRoomId | null> {
if (config.debug) {
console.log(
Expand Down Expand Up @@ -140,6 +116,30 @@ class Matrix extends Integration {
return user.externalIds.matrixRoomId
}

public async sendMessageInRoom(
roomId: MatrixRoomId,
message: string
): Promise<void> {
if (config.debug) {
console.log(`Matrix notification skipped (debug mode): ${message}`)
return
}
return axios
.post(
`https://${this.credentials.host}/_matrix/client/r0/rooms/${roomId}/send/m.room.message?access_token=${this.credentials.token}`,
{
msgtype: 'm.text',
format: 'org.matrix.custom.html',
body: message.replace(this.tagsRe, ''),
formatted_body: message,
},
{
headers: this.headers,
}
)
.then(() => {})
}

public async inviteUserInRoom(
roomId: MatrixRoomId,
username: MatrixUsername
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// @ts-check
const { Sequelize, DataTypes } = require('sequelize')

module.exports = {
async up({ context: queryInterface, appConfig }) {
await queryInterface.sequelize.transaction(async (transaction) => {
await queryInterface.addColumn(
'working_hours_entries',
'autoGenerated',
{
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
{ transaction }
)

await queryInterface.sequelize.query(
`
UPDATE working_hours_entries
SET "autoGenerated" = (metadata->>'auto')::boolean
WHERE metadata->>'auto' IS NOT NULL
`,
{ transaction }
)
})
},

async down({ context: queryInterface, appConfig }) {
await queryInterface.sequelize.transaction(async (transaction) => {
await queryInterface.removeColumn(
'working_hours_entries',
'autoGenerated',
{
transaction,
}
)
})
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class WorkingHoursEntry
declare startTime: WorkingHoursEntryModel['startTime']
declare endTime: WorkingHoursEntryModel['endTime']
declare metadata: WorkingHoursEntryModel['metadata']
declare autoGenerated: WorkingHoursEntryModel['autoGenerated']
}

WorkingHoursEntry.init(
Expand Down Expand Up @@ -55,6 +56,11 @@ WorkingHoursEntry.init(
type: DataTypes.JSONB,
defaultValue: {},
},
autoGenerated: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
createdAt: DataTypes.DATE,
updatedAt: DataTypes.DATE,
},
Expand Down
5 changes: 4 additions & 1 deletion src/modules/working-hours/server/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,10 @@ const userRouter: FastifyPluginCallback = async function (fastify, opts) {
}
const newEntries: Array<
Pick<WorkingHoursEntry, 'userId' | 'date' | 'startTime' | 'endTime'>
> = req.body.map((x) => ({ ...x, userId: req.user.id }))
> = req.body.map((x) => ({
...x,
userId: req.user.id,
}))
let error: string | null = null

// validate each entry
Expand Down
1 change: 1 addition & 0 deletions src/modules/working-hours/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface WorkingHoursEntry {
startTime: string
endTime: string
metadata: Record<string, unknown>
autoGenerated: boolean
createdAt: Date
updatedAt: Date
}
Expand Down

0 comments on commit 6d82369

Please sign in to comment.