Created
November 22, 2022 19:58
-
-
Save Przemocny/d58270503302cf1de7c1648a41ea554e to your computer and use it in GitHub Desktop.
HRy LHA / Umowy / Etap 10 / Łączenie podpisanych dokumentów z odpowiednim wpisem i informowanie księgowej o skończeniu procesu podpisywania
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const registryConfig = { | |
name: 'REJESTR UMÓW', | |
fields: { | |
month: 'MSC', | |
person: 'LUDZIE', | |
deal: 'NIEPODPISANA UMOWA', | |
paycheck: 'NIEPODPISANY RACHUNEK', | |
signedDeal: 'PODPISANA UMOWA', | |
signedPaycheck: 'PODPISANY RACHUNEK', | |
status: 'STATUS DLA AUTOMATYZACJI', | |
dealType: 'TYP UMOWY (from LUDZIE)' | |
} | |
} | |
function getEntryFields(record, fields) { | |
return fields.reduce((sum, field) => { | |
sum[field] = record.getCellValue(field) | |
return sum | |
}, {}) | |
} | |
async function getAllDuplicates() { | |
const table = base.getTable(registryConfig.name); | |
const { records } = await table.selectRecordsAsync({ | |
fields: Object.values(registryConfig.fields), | |
sorts: [{ | |
field: 'LUDZIE', | |
direction: 'asc' | |
}] | |
}) | |
const notConfirmedRecords = new Map() | |
const newRecordsFromForm = new Map() | |
for (const record of records) { | |
const copy = getEntryFields(record, Object.values(registryConfig.fields)) | |
const flowNotRequired = ['B2B', 'UOP', 'NIEOKREŚLONA'].includes(copy['TYP UMOWY (from LUDZIE)'][0].name) | |
if (flowNotRequired) { | |
continue | |
} | |
const isNewRecord = copy['STATUS DLA AUTOMATYZACJI'] == null | |
const patternsToCompare = `${copy['MSC']}_${copy['LUDZIE'][0].id}` | |
if (isNewRecord) { | |
const signedDeal = record.getCellValue(registryConfig.fields.signedDeal) | |
const signedPaycheck = record.getCellValue(registryConfig.fields.signedPaycheck) | |
newRecordsFromForm.set(patternsToCompare, { | |
id: record.id, | |
signedDeal, | |
signedPaycheck | |
}) | |
continue | |
} | |
const isProperStepInAutomation = copy['STATUS DLA AUTOMATYZACJI']?.name == 'WPISANA W KADRACH' | |
if (!isProperStepInAutomation) { | |
continue | |
} | |
notConfirmedRecords.set(patternsToCompare, record.id) | |
} | |
for (const [key, id] of notConfirmedRecords.entries()) { | |
const newEntry = newRecordsFromForm.get(key) | |
if (newEntry) { | |
await table.updateRecordAsync(id, { | |
[registryConfig.fields.signedDeal]: newEntry.signedDeal, | |
[registryConfig.fields.signedPaycheck]: newEntry.signedPaycheck, | |
'STATUS DLA AUTOMATYZACJI':{name:'DOKUMENTY PODPISANE PRZEZ PRACOWNIKA'} | |
}) | |
await table.deleteRecordAsync(newEntry.id) | |
console.log('updated record', id) | |
console.log('deleted record', newEntry.id) | |
} | |
} | |
} | |
await getAllDuplicates() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment