Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DT-M3] Create dt-maarifa API to receive contact and interaction data #2

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

adventureit
Copy link

The Maarifa RS currently uses built-in DT APIs to create contacts and comments. This mostly suits our needs right now, but some options to expand the Maarifa Plugin features could use their own API.

Creating 3 endpoints just for the Maarifa Plugin that the RS will send data to directly and DT can handle some special cases better.

-POST /wp-json/dt-maarifa/v1/contacts
-POST /wp-json/dt-maarifa/v1/contacts/:id
-POST /wp-json/dt-maarifa/v1/contacts/:id/interactions

3 endpoints created.
1st - Create new contact/update if maarifa_data already exists - Ok, if using DT json formating;
2nd - Update based on DT id - Ok, if using DT json formating;
3rd - Create/update interactions - In progress. Create and update are Ok, if using DT json formating, but needs to add some fields.

It also remains to map the DT x Maarifa fields and use it before performing the above actions.
… - Maping Maarifa x DT fields

Creating function mapFieldsToContact to map Maarifa x DT Fields and contemplate differences and exceptions.
Mapping Maarifa x DT fields and correcting adding and updating comments.
Corrections on Mapping Maarifa x DT fields (tags and milestones).
@cairocoder01
Copy link
Collaborator

cairocoder01 commented Oct 2, 2024

@adventureit Nice work getting this started. I tested the first endpoint and found a number of problems, so I didn't test the others yet since they will probably have the same ones. Once the bugs below are fixed, I can test the other 2 endpoints and look at the code in more detail.

Simple Contact

When sending to POST /wp-json/dt-maarifa/v1/contacts

{
    "id": 12345,
    "name": "John Doe",
    "email": "john.doe@test.com",
    "phone": "+1-9995551234",
    "street": "South Pole",
    "country": "antarctica",
    "gender": "male",
    "age": "35-44",
    "notes": "General info about contact"
}

I get the error:

{
    "code": "create_post",
    "message": "'notes' field expected to be an array",
    "data": null
}

When sending to POST /wp-json/dt-maarifa/v1/contacts
with simple data above (without notes property)

I get the error:

{
    "code": "update_multi_select_fields",
    "message": "missing values field on: milestones",
    "data": {
        "status": 400
    }
}

However, it does create a contact and will succeed on the second try.

After the second try, I noticed these bugs:

  1. the contact does not have the street/country values in the Locations or Address fields
  2. the maarifa_data field is set to the id but should be the whole contact json object (this will cause the Maarifa tile on the contact screen to show the id and other info)
  3. the contact should have a source of 'maarifa' added
  4. the age field is not being set

Full

When sending to POST /wp-json/dt-maarifa/v1/contacts

{
    "id": 45235,
    "name": "Jon Doe \/ Bob Johnson",
    "email": "[\"bobscott.explorer@gmail.com\",\"test@gmail.com\"]",
    "phone": "[\"+44-1234567890\",\"+1-9995551235\"]",
    "phonecode": "+44",
    "skype": "",
    "facebook": "",
    "street": "South Pole",
    "country": "antarctica",
    "ip_city": null,
    "ip_city_id": null,
    "gender": "male",
    "age": "45+",
    "background": "christian",
    "spiritual": "seeker",
    "tags": [{
        "id": 11,
        "name": "\u0623\u062d\u0644\u0627\u0645 \u0631\u0648\u062d\u064a\u0629",
        "alias": "spiritual-dreams"
    }, {
        "id": 19,
        "name": "\u0627\u0628\u0646 \u0627\u0644\u0633\u0644\u0627\u0645",
        "alias": "person-of-peace"
    }],
    "notes": "testest",
    "created": 1602661663,
    "last_updated": 1710502175,
    "last_seen": 1710502175,
    "first_contact": "whatsapp",
    "first_contact_details": null,
    "first_source": "-",
    "ds_user_id": "",
    "external_id": "20",
    "external_url": "https:\/\/maarifa.journey.tools\/demo",
    "location_details": null,
    "milestones": {
        "has bible": true,
        "studying": true,
        "profession": false,
        "can share": false,
        "sharing": false,
        "baptized": false,
        "baptizing": false,
        "in group": false,
        "starting groups": false
    }
}

I am noticing these bugs:

  1. phone numbers are not split
  2. emails are not split
  3. location not set
  4. age not set
  5. Missing source of Maarifa
  6. maarifa_data set to id instead of full object
  7. Notes fails with message about needing to be an array. Removed that property to test the above.

Code Formatting

You might notice that on this pull request, there is a notice that checks failed. If you click into that, you should be able to see that the PHPCS (PHP code sniffer) - which makes sure the code is formatted in a consistent way - is failing. You can run that check locally by using these commands on your command line:

composer install
composer run lint
composer run lintfix

The third one will try to automatically fix whatever it's able to. Mostly it fixes spacing of the code. The second will tell you exactly what other issues there are. Let me know if any of them are hard to understand.

@cairocoder01 cairocoder01 self-assigned this Oct 2, 2024
Fixing bugs:
-maarifa_data field
-age field
-phone numbers are not split
-emails are not split
-age not set
-maarifa_data set to id instead of full object
-Notes fails
-Code Formatting
@adventureit
Copy link
Author

Hi, Jon. Thanks for your review and support.
Bugs fixed, except location (still working on it) and source = maarifa. I do not have Maarifa as an option on my sources list. Should i have it or need do add it somehow?

Thank you.

image

Copy link
Collaborator

@cairocoder01 cairocoder01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adventureit This is working much better now! Nicely done! Adding a contact seems to work great and updating contacts also seems to work well. I wasn't able to add an interaction/comment, but I'm guessing you haven't worked much on that part yet.

I left a few code comments of some things that can be cleaned and a few bugs.

Comment on lines +282 to +294
if ( !empty( $contact_map['tags'] ) )
{

$int_count0 = 0;

foreach ( $contact_map['tags'] as $key => $value ) {

$fields_map['tags']['values'][$int_count0]['value'] = $contact_map['tags'][$key]['alias'];

$int_count0++;

}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but there's two things that could clean it up:

  • I don't think you need the int_count0 variable. You can just assign to $fields_map['tags']['values'][] = [ 'value' => $value['alias']. That [] means to append a new item to the array.
  • In that code above, you'll see that I used $value instead of $contact_map['tags'][$key] since it should be the same thing from how you wrote the foreach statement. You get the key and the value, so you can just directly use the value.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this suggestion hasn't been changed yet.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something happened in my last commit and I lost the changes. I redid tit and just made a new commit.


if ( !empty( $contact_map['milestones'] ) ) {

$int_count = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above about not needing the int_count. You can just write [] to append a new item to an array.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this suggestion hasn't been changed yet.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something happened in my last commit and I lost the changes. I redid tit and just made a new commit.

Fixing bugs in location (country), interactions, update (returning post), and adding comment.
Copy link
Collaborator

@cairocoder01 cairocoder01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there. I just noticed a bug that if you don't send a milestones property in the API body when doing an update, you get an activity message in the DT UI that says "0 added to Faith Milestones"

Comment on lines +282 to +294
if ( !empty( $contact_map['tags'] ) )
{

$int_count0 = 0;

foreach ( $contact_map['tags'] as $key => $value ) {

$fields_map['tags']['values'][$int_count0]['value'] = $contact_map['tags'][$key]['alias'];

$int_count0++;

}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this suggestion hasn't been changed yet.


if ( !empty( $contact_map['milestones'] ) ) {

$int_count = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this suggestion hasn't been changed yet.

}
else //No milestones
{
$fields_map['milestones']['values'][1]['value'] = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the intention with this code? I noticed that if I didn't send the milestones property in the API request, the activity comment in DT said "0 added to Faith Milestones".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants