|
// To use any npm package on Pipedream, just import it |
|
import moment from 'moment'; |
|
import axios from "axios"; |
|
|
|
export default defineComponent({ |
|
async run({ steps, $ }) { |
|
|
|
const today = moment( new Date() ).format("YYYY-MM-DD"); |
|
console.log('🔆 Getting data for date: ' + today) |
|
|
|
const data = await getRizeWorkHours(today); |
|
console.log('⏰ Data received:', data); |
|
|
|
// You need to have created these measures in Conjure.so! |
|
// Measure Type = Time Entry. |
|
const conjureMeasures = { |
|
workHours: 'meas_PEpOo2wPuYIayt', |
|
trackedTime: 'meas_YUFIKHuM7xu6pn', |
|
focusTime: 'meas_eojMpWxI2wcFQT', |
|
meetingTime: 'meas_g35McMEtmUuHGd', |
|
breakTime: 'meas_JjQOUldQkbbx0D' |
|
} |
|
|
|
await createConjureMeasurements( today, data, conjureMeasures ); |
|
|
|
return data |
|
|
|
}, |
|
}) |
|
|
|
async function getRizeWorkHours(datetime){ |
|
// this request was copied from rize devtools like this: https://share.cleanshot.com/NiMyHI |
|
const options = { |
|
method: 'POST', |
|
url: 'https://api.rize.io/v1/graphql', |
|
headers: { |
|
'Content-Type': 'application/json', |
|
'access-token': process.env.RIZE_ACCESS_TOKEN, |
|
'client': process.env.RIZE_CLIENT, |
|
'uid': process.env.RIZE_UID |
|
}, |
|
data: { |
|
'operationName': 'WorkHours', |
|
'variables': '{ "date": "'+datetime +'" }', |
|
'query': "query WorkHours($date: String!) { workHours(date: $date) { workHours trackedTime focusTime meetingTime breakTime } }", |
|
} |
|
}; |
|
let responseData = await axios.request(options); |
|
return responseData.data.data.workHours; |
|
} |
|
|
|
async function createMeasure(measurementId, measurementTimestamp, measurementValue, measurementComment){ |
|
const options = { |
|
method: 'POST', |
|
url: 'https://api.conjure.so/graphql', |
|
headers: { |
|
'Authorization': 'Bearer ' + process.env.CONJURE_TOKEN, |
|
'Content-Type': 'application/json' |
|
}, |
|
data: { |
|
"query": "mutation{ measurementCreate( input:{ measureId: \""+measurementId+"\", attributes: { timestamp: \""+measurementTimestamp+"\", comment: \""+measurementComment+"\", values: { active: false, duration: "+measurementValue+" } } } ) { success }}" |
|
} |
|
}; |
|
let responseData = await axios.request(options) |
|
return responseData.data; |
|
} |
|
|
|
async function createConjureMeasurements( today, data, measures ){ |
|
for(const currentMeasure in measures){ |
|
const measurementId = measures[currentMeasure]; |
|
const measurementTimestamp = today; |
|
const measurementValue = data[currentMeasure]; |
|
const measurementComment = '🤖 Programatically adding ' + currentMeasure + ' of ' + today; |
|
const measure = await createMeasure(measurementId, measurementTimestamp, measurementValue, measurementComment); |
|
console.log(measurementComment, measure); |
|
} |
|
} |