amo/scripts/inn-check.xgo

118 lines
2.3 KiB
Text

os := import("os")
fmt := import("fmt")
json := import("json")
args := os.args()[2:]
file_path := args[0]
contact_field_id := 231711
company_field_id := 1300614
lead_field_id := 686597
holders := json.decode(os.read_file(file_path))
//fmt.println(len(holders))
//os.exit(0)
patch := []
find_by_field_id := func(id, fields){
for _, field in fields {
if field.field_id == id {
return field
}
}
}
is_field_correct := func(field){
if !int(field.values[0].value) {
return false
}
str := string(field.values[0].value)
if len(str) <9 || len(str) > 12 {
return false
}
return true
}
//holders = holders[:2]
for i, h in holders {
company := {}
if !h.company_id {
continue
}
company.id = h.company_id
company_inn := find_by_field_id(company_field_id, h.company_inns)
//fmt.println(i, " reachd", company_inn)
if company_inn {
continue
}
contact_inn := find_by_field_id(contact_field_id, h.contact_inns)
lead_inn := find_by_field_id(lead_field_id, h.lead_inns)
if !contact_inn {
if !lead_inn || !is_field_correct(lead_inn) {
continue
}
company.__reason = "Взято из лида (в контакте не было)"
company.custom_fields_values = [{
field_id: company_field_id,
values: [{
value: string(lead_inn.values[0].value)
}]
}]
patch += [company]
continue
}
if !lead_inn {
if !is_field_correct(contact_inn) {
continue
}
company.__reason = "Взято из контакта (в лиде не было)"
company.custom_fields_values =[{
field_id: company_field_id,
values: [{
value: string(contact_inn.values[0].value)
}]
}]
} else {
if int(contact_inn.values[0].value) != int(lead_inn.values[0].value) {
continue
}
if !is_field_correct(contact_inn) {
continue
}
company.__reason = "Взято из лида и контакта"
company.custom_fields_values =[{
field_id: company_field_id,
values: [{
value: string(contact_inn.values[0].value)
}]
}]
}
patch += [company]
/*if h.company_inns {
for _, field in h.company_inns {
company.custom_fields_values += [{
field_id: field.field_id,
values: [{
value: field.values[0].value
}]
}]
}
}
companies += [company]*/
}
//patch = patch[:500]
//fmt.println(len(patch))
bts := json.indent(json.encode(patch), "", " ")
fmt.println(string(bts))