Exemplo: Mapear o registro de trabalho do Jira para o tempo do Targetprocess
Transforma o registro de trabalho da questão do Jira em entidade de tempo do Targetprocess. Quando o worlkog é alterado, exclua todos os registros de tempo da entidade de destino e crie um novo com o tempo gasto/restante do Jira.
Transformação do Jira para o Targetprocess:
if (!args.value.changed) {
return
}
const proxySvc = context.getService('workSharing/v2')
const tpApi = context.getService('targetprocess/api/v2')
const proxy = proxySvc.getProxy(args.targetTool)
const timeResponse = await tpApi.queryAsync('time', {
where: ___PROTECTED_1___
})
const tpEntityId = parseInt(args.targetEntity.sourceId)
const entityResponse = await tpApi.getByIdAsync(args.targetEntity.entityType, tpEntityId, {
select: '{{owner.id,owner.kind} as owner, project}'
})
const { owner, project } = entityResponse
if (project) {
const timeRecordOwnerId = owner && owner.kind.toLowerCase() === 'user' ? owner.id : 1 // set any default user id which will be used as time records onwer here
const deleteCommands = timeResponse.map(tr => ({
name: 'DeleteResource',
target: {
Id: tr.id,
ResourceType: 'Time'
}
}))
// developer
const role = {
id: 1
}
const createCommand = {
name: 'createResource',
resource: {
resourceType: 'Time',
assignable: { id: tpEntityId },
project: { id: project.id },
user: { id: timeRecordOwnerId },
description: 'Created by ILI integration',
spent: args.value.changed.timeSpentSeconds / 3600,
remain: args.value.changed.remainingEstimateSeconds / 3600,
role
}
}
const batchCommand = {
name: 'batch',
commands: [...deleteCommands, createCommand]
}
await proxy.postAsync('/api/commands/v1/execute', {body: batchCommand})
}
Versão na nuvem:
if (!args.value.changed) {
return
}
const proxySvc = context.getService('workSharing/v2')
const tpApi = context.getService('targetprocess/api/v2')
const proxy = proxySvc.getProxy(args.targetTool)
const timeResponse = await tpApi.queryAsync('time', {
where: ___PROTECTED_2___
})
const tpEntityId = parseInt(args.targetEntity.sourceId)
const entityResponse = await tpApi.getByIdAsync(args.targetEntity.entityType, tpEntityId, {
select: '{{owner.id,owner.kind} as owner, project}'
})
const { owner, project } = entityResponse
if (project) {
const timeRecordOwnerId = owner && owner.kind.toLowerCase() === 'user' ? owner.id : 1 // set any default user id which will be used as time records onwer here
const deleteCommands = timeResponse.map(tr => ({
name: 'DeleteResource',
target: {
Id: tr.id,
ResourceType: 'Time'
}
}))
// developer
const role = {
id: 1
}
const timeSpentSeconds = args.value.changed.timeSpentSeconds ?? 0
const timeremainingEstimateSeconds = args.value.changed.remainingEstimateSeconds ?? 0
const createCommand = {
name: 'createResource',
resource: {
resourceType: 'Time',
assignable: { id: tpEntityId },
project: { id: project.id },
user: { id: timeRecordOwnerId },
description: 'Created by ILI integration',
spent: timeSpentSeconds / 3600,
remain: timeremainingEstimateSeconds / 3600,
role
}
}
const batchCommand = {
name: 'batch',
commands: [...deleteCommands, createCommand]
}
await proxy.postAsync('/api/commands/v1/execute', {body: batchCommand}).catch(e=> {
console.log(e);
})
}
Mais exemplos na GitHub biblioteca de exemplos de mapeamentos e roteamentos JS.