# Purgatory Customer Healing Script Psuedo Code:
## Globals
1. Mapping between POS Vendor names in Excel doc and the pos_vendor_id in our dbs
- preferably this global mapping is validated against the ingested Excel doc and raises if a POS name does not exist in the mapping
## Main
1. Ingest all SFDC client records
3. for each row
1. get client via SFDC ID.
1. if one client exists for SFDC ID:
1. update pos and saas agreement status on client.
2. IF client.pos_vendor_id is NULL && SFDC client record contains a POS vendor that is NOT "Unknown"
1. **update pos_vendor_id using the global map. IF map does not contain POS. keep "unknown".**
3. fetch client_setup_status via client_id
4. IF CSS DOES NOT EXIST:
1. create CSS record
2. set expected state based on saas agreement and pos agreement
7. ELSE
1. set expected state based on saas agreement and pos agreement
2. save CSS changes to color DB and push CSS changes to EDO
3. fetch user via client_id
4. IF USER DOES NOT EXIST:
5. Create user via email and set a new password. When the user is ready for a tokenized link we MAY need to manually fire the route that wil do that.
2. if no client exists for SFDC ID
1. Use /signup route logic to:
1. create client (new uuid) along with pos and saas agreement set correctly
2. create client setup status record that is correctly sets signed saas and signed POS
3. creates user
4. Creates org levels
5. creates national org unit
6. pushes all changes to EDO
3. if multi client
1. log WARNING to **capture SFDC IDs** that contain the multi client records
2. skip client and continue...
2. Check SFDC client record for SaaS agreement GDoc link
1. if includes a link:
1. IF agreement_signed table contains a SaaS agreement for the client (template_id: 1)
1. Do not upload the file
2. ELSE
1. upload blob to GFS to proper bucket+client+file naming convention
```python
dst_file_name = f"{client.id}/{saas_agreement_id}-{uuid4()}{file_extension}"
```
2. insert agreement_signed record for tempalte_id 1 and where other details cannot be null, use "LEGACY"
3. push changes to EDO via endpoint
```sql
INSERT INTO common_etl.agreement_signed (client_id,template_id,agreement_uri,ip_address,agent_string,email,first_name,last_name,created_by,last_updated_by,created_datetime,last_updated_datetime,record_source) VALUES
('5ba5a5db-22ba-449f-978a-0b814fd9228e',1,'gs://gcp-tqt-prd-app-agreement-signed/5ba5a5db-22ba-449f-978a-0b814fd9228e/1-a7c15ca5-17cb-4fdd-acb2-7e4df2d09ef6.pdf','LEGACY','LEGACY','fred3@allentire.com','Allen Tire Company','Allen Tire Company',NULL,NULL,'2020-11-04 13:18:36.515','2020-11-04 13:18:36.515','database');
```
1. Check if SFDC client record contains a POS agreement GDoc link
1. if includes a link:
1. IF agreement_signed table contains a POS agreement for the client (any template ID)
1. Do not upload the file
2. ELSE
1. upload blob to GFS to proper bucket+client+file naming convention (as the example above shows)
2. fetch the proper agreement_template ID based on the pos_vendor_id.
3. insert agreement_signed record for tempalte_id fetched in previous step and where other details cannot be null, use "LEGACY"
4. push changes to EDO via endpoint