Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- onFileDropped(event) {
- const results = [];
- if (!this.personsPresenceService.readOnly) {
- for (let i = 0; i < event.length; i++) {
- const file = event[i];
- if (
- file.type === 'application/pdf' ||
- file.type === 'image/jpeg' ||
- file.type === 'image/png'
- ) {
- this.personsPresence.analysing = true;
- this.personsPresenceService.analysis(file).subscribe(
- (detectedInfo: any) => {
- // make document
- if (detectedInfo.Expiration) {
- const dmy = detectedInfo.Expiration.split('/');
- detectedInfo.Expiration = new Date(dmy[2], dmy[1] - 1, dmy[0]);
- }
- if (detectedInfo.Issue) {
- const dmy = detectedInfo.Issue.split('/');
- detectedInfo.Issue = new Date(dmy[2], dmy[1] - 1, dmy[0]);
- }
- const document = {
- data: {
- name: file.name,
- document_type_id: detectedInfo.detected_types,
- expiration: detectedInfo.Expiration,
- documentDate: detectedInfo.Issue,
- traxxeo_ai_verified: 'Y',
- lm_name: this.userDataService.username,
- validated: true,
- validation_date: new Date(),
- validation_gui_user_id: localStorage.guiUserId,
- validation_person: this.userDataService.username,
- file
- }
- };
- // find the person attached to this file
- let belongPerson = this.ListPersonGridComponent.grid.data.find(
- (person) =>
- (person.niss &&
- person.niss === detectedInfo.NISS &&
- (!detectedInfo.BCE ||
- person.real_employer_tin === detectedInfo.BCE)) ||
- (person.limosa && person.limosa === detectedInfo.limosa)
- );
- const result = {
- belongPerson,
- fileData: document.data,
- detectedInfo,
- newPerson: false
- };
- if (belongPerson) {
- results.push(result);
- // check document
- const verified = this.personsPresence.checkDocument(
- detectedInfo,
- belongPerson.data.niss
- );
- document.data.traxxeo_ai_verified = verified;
- // update person according to info find on doc
- if (verified === 'Y') {
- this.personsPresence.updateDetectedInfoPerson(
- belongPerson,
- detectedInfo
- );
- }
- this.personsPresenceService
- .updatePerson(belongPerson)
- .subscribe(
- () => {
- // add document to the person attached to this file
- this.personsPresenceService
- .addAttachDoc(document, belongPerson.data.person_id)
- .subscribe(
- () => {
- // sync with DB for updated person
- this.personsPresenceService
- .getPersons(belongPerson.data.person_id, null)
- .then(
- (persons: any) => {
- const personUpdated = persons.items[0];
- Object.assign(
- this.personsPresence.persons.find(
- (person) =>
- person.person_id ===
- personUpdated.person_id
- ),
- personUpdated
- );
- this.personsPresence.parsePerson(
- personUpdated
- );
- this.ListPersonGridComponent.grid.store.add(
- personUpdated
- );
- this.ListPersonGridComponent.grid.store.filter();
- },
- (error) => {
- this.notificationService.errorToast(
- 'get person',
- error
- );
- }
- );
- },
- (error) => {
- this.notificationService.errorToast(
- 'new doc',
- error
- );
- }
- );
- },
- (error) => {
- this.notificationService.errorToast(
- 'update person',
- error
- );
- }
- );
- } else if (detectedInfo.NISS || detectedInfo.limosa) {
- // create new person
- belongPerson = {
- last_name: detectedInfo.lastName,
- first_name: detectedInfo.firstName,
- name: detectedInfo.lastName + ' ' + detectedInfo.firstName,
- niss: detectedInfo.NISS,
- real_employer_name: detectedInfo.companyName,
- real_employer_tin: detectedInfo.BCE,
- limosa: detectedInfo.limosa,
- category_name: this.personsPresence.getDefaultCategory().name,
- person_status_id: 0,
- is_blacklisted: 'N'
- };
- result.belongPerson = { data: belongPerson };
- result.newPerson = true;
- results.push(result);
- this.personsPresenceService
- .createPerson(belongPerson)
- .subscribe(
- (res: any) => {
- this.personsPresenceService
- .addAttachDoc(document, +res.person_id)
- .subscribe(
- () => {
- // sync with DB for updated person
- this.personsPresenceService
- .getPersons(+res.person_id, null)
- .then(
- (persons: any) => {
- const personUpdated = persons.items[0];
- this.personsPresence.persons.push(
- personUpdated
- );
- this.personsPresence.parsePerson(
- personUpdated
- );
- this.ListPersonGridComponent.grid.store.add(
- personUpdated
- );
- this.ListPersonGridComponent.grid.selectRow(
- { record: personUpdated.id },
- true
- );
- this.ListPersonGridComponent.grid.store.filter();
- },
- (error) => {
- this.notificationService.errorToast(
- 'get person',
- error
- );
- }
- );
- },
- (error) => {
- this.notificationService.errorToast(
- 'new doc',
- error
- );
- }
- );
- },
- (error) => {
- this.notificationService.errorToast('new person', error);
- }
- );
- } else {
- // unattached document
- results.push(result);
- }
- if (i === event.length - 1) {
- this.openDropResult(results);
- this.personsPresence.analysing = false;
- }
- },
- (error) => {
- if (i === event.length - 1) {
- this.openDropResult(results);
- this.personsPresence.analysing = false;
- }
- this.notificationService.errorToast('analysis', error);
- }
- );
- } else {
- const document = {
- data: {
- name: file.name,
- file
- }
- };
- results.push({
- belongPerson: null,
- fileData: document.data,
- detectedInfo: null,
- newPerson: false
- });
- if (i === event.length - 1) {
- this.openDropResult(results);
- this.personsPresence.analysing = false;
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment