Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions AutomaticClockOuts/clock_out.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,27 @@ const config = require("./config");
const TimeEntry = require('./time_entry');
const axios = require('axios');
const MsalClient = require('./msal_client')
const TimeEntryDao = require('./time_entry_dao')

const doClockOut = async (context, timer) => {
const doClockOut = async (context) => {
context.log(`I am going to check how many entries were not clocked out ${new Date()}`);
const {endpoint, key, databaseId, containerId, slackWebHook} = config;

const { endpoint, key, databaseId, slackWebHook } = config;
const client = new CosmosClient({endpoint, key});
const database = client.database(databaseId);
const container = database.container(containerId);
const timeEntryDao = new TimeEntryDao(database);

const response = await MsalClient.findUsersInMS();
const users = response.data.value;

const QUERY_WITHOUT_END_DATE =
"SELECT * FROM c WHERE (NOT IS_DEFINED(c.end_date) OR IS_NULL(c.end_date) = true) AND IS_DEFINED(c.start_date)"

const {resources: entries} = await container.items
.query({query: QUERY_WITHOUT_END_DATE})
.fetchAll();

const {resources: entries} = await timeEntryDao.getEntriesWithNoEndDate();
context.log(`Checking for time-entries that need to be clocked out`);

let totalClockOutsExecuted = 0;
const usersWithClockOut = []
await Promise.all(entries.map(async (timeEntryAsJson) => {
const timeEntry = new TimeEntry(timeEntryAsJson)
if (timeEntry.needsToBeClockedOut()) {
usersWithClockOut.push(findUser(users, timeEntry.timeEntry.owner_id))
usersWithClockOut.push(findUser(users, timeEntry.timeEntry.owner_id));
timeEntryAsJson.end_date = timeEntry.getTimeToClockOut()
await container.item(timeEntryAsJson.id, timeEntryAsJson.tenant_id).replace(timeEntryAsJson)
totalClockOutsExecuted++
Expand Down
1 change: 0 additions & 1 deletion AutomaticClockOuts/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ const config = {
endpoint: process.env["ENDPOINT"],
key: process.env["KEY"],
databaseId: "time-tracker-db",
containerId: "time_entry",
partitionKey: { kind: "Hash", paths: ["/category"] },
clientId: process.env["CLIENT_ID"],
authority: process.env["AUTHORITY"],
Expand Down
5 changes: 0 additions & 5 deletions AutomaticClockOuts/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
const CosmosClient = require("@azure/cosmos").CosmosClient;
const config = require("./config");
const TimeEntry = require('./time_entry');
const axios = require('axios');
const MsalClient = require('./msal_client');
const ClockOut = require('./clock_out');

module.exports = async function (context, myTimer) {
Expand Down
10 changes: 6 additions & 4 deletions AutomaticClockOuts/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ NOTE:
Don't forget to set the following environment variables to make this app work:

```sh
COSMOS_DB_KEY
MS_CLIENT_ID
MS_AUTHORITY
MS_CLIENT_SECRET
ENDPOINT='XXX'
KEY='XXX'
CLIENT_ID='XXX'
AUTHORITY='XXX'
CLIENT_SECRET='XXX'
SLACK_WEBHOOK='XXX'
```
Check the pinned message in our slack channel to get these values
18 changes: 18 additions & 0 deletions AutomaticClockOuts/time_entry_dao.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class TimeEntryDao {

constructor(database) {
const CONTAINER_ID = 'time_entry';
this.container = database.container(CONTAINER_ID);
}

async getEntriesWithNoEndDate () {
const QUERY_WITHOUT_END_DATE =
"SELECT * FROM c WHERE (NOT IS_DEFINED(c.end_date) OR IS_NULL(c.end_date) = true) AND IS_DEFINED(c.start_date) AND (NOT IS_DEFINED(c.deleted) OR IS_NULL(c.deleted) = true)";
return this.container.items
.query({query: QUERY_WITHOUT_END_DATE})
.fetchAll();
}

}

module.exports = TimeEntryDao