SAPUI5 & ODataModel V2: Find out which request groups have pending requests - odata

I'm using ODataModel V2 and have 3 deferred request groups. If there are requests for more than one group I have to submit them in specific order.
Therefore I need to know which group has pending requests. Is there already a smart method for?
Definition of my request groups
Entry creations via Method "createEntry" are collected into group "create"
Entry deletions via Method "remove" are collected into group "delete"
Everything else is collected into group "changes"
What I've found out so far
The "delete" group is easy, if there is one, it's contained in "oDataModel.mDeferredRequests".
The "create" group is only contained in "mDeferredRequests" if there are also requests for the "changes" group. So if it's contained in "mDeferredRequests" I know that there are requests for both "create" and "changes".
But if there are only requests for either "create" or "changes" I've found only the way via method "getPendingChanges".
The newly created entities have the groupId included within the metadata like so:
NewObject('id-123456-00'):
Attribute: "123"
__metadata:
created:
changeSetId: "chgSet"
eTag: undefined
error: undefined
groupId: "create"
...
type: "..."
uri: "/sap/opu/odata/sap/..."
The changed entities have no groupId, they look like so:
UpdatedObject(Key='123'):
Attribute: "abc"
__metadata:
id: ".../sap/opu/odata/sap/..."
type: "..."
uri: ".../sap/opu/odata/sap/..."
So my only idea at the moment is to loop over the entries from "getPendingChanges" and check if there's a groupId or not.
As I'm not happy with this solution I'd like to ask you if there's a smarter way to determine the groups with pending requests.

Related

How to change Rails object fields after it's been paid via Stripe Checkout?

In my application, a user can create a draft of an object, and then on a "finalise" view they can check if they only want the standard price or one of the two extra features as well (e.g. their entry being featured on the platform) - these are stored as three separate products each with their own price because I want to be able to track them separately in the future.
I am using Checkout for payments and my backend is Rails.
I am listening to webhooks, but I don't know how I could actually modify the object in question, since I can't see any webhook request body that contains the information I need.
What I imagine I'd need is something like this: "there has been a successful charge of $x related to your object with id 123, and the charge included product prod_asdf and prod_sdfg", and then I could update e.g. the paid and featured boolean fields of my object.
This seems like a no-brainer, since the products and prices are actually handled by Stripe and they are passed to the checkout session, I don't see why I can't access them from the webhooks?
EDIT: I'm wondering if passing necessary metadata to the Session object and then using the same metadata listening to a checkout.session.completed event is a good idea.
You can link a Checkout Session back to your Listing object by including the related object ID in the metadata (https://stripe.com/docs/api/checkout/sessions/object#checkout_session_object-metadata) when you create the Checkout Session. When you listen for checkout.session.completed events, the event will come with a Checkout Session object. You can easily link the Checkout Session back to your Listing object by checking the metadata and it also has an amount_total field so that will tell you the total payment amount.
You can get the products included in a session from line_items (https://stripe.com/docs/api/checkout/sessions/object#checkout_session_object-line_items). Unfortunately, line_items is not included in the webhook event by default since it is expandable (https://stripe.com/docs/expand). After receiving the checkout.session.completed event you can use the ID to retrieve the Checkout Session with line_items expanded like this:
session = Stripe::Checkout::Session.retrieve(
id: 'cs_test_xxx',
expand: ['line_items'],
)
line_items = session.line_items
Alternatively, you can just retrieve the Checkout Session's line items (https://stripe.com/docs/api/checkout/sessions/line_items) like this:
line_items = Stripe::Checkout::Session.list_line_items('cs_test_D3OF4MY1VGflR8idkewd795t8MLd4PwA3wxdLCHikdRFTIkBecH6FKij', {limit: 5})

Implementing Pusher Chat in Rails and React

I'm using this repo to create a chat system between 2 users in a Rails and React project. I've been able to log the user input into the console, and I have created messages_controller and message_threads_controller according to the repo.
However, I'm unable to persist the message to Rails db and then authenticate a user with Pusher before sending it to Pusher. Mainly because the from_uid, to_uid and thread_uid are not present by the time the message is been sent to Rails. Sending the message to rails like this:
sendMessage = (event) => {
event.preventDefault();
const {message} = this.state;
axios.post('/api/v1/messages', {message: message})
.then((response) => {
console.log(response);
})
console.log('send Message')
this.setState({'message': message});
console.log(this.state.message);
}
In my routes.rb file I have this
resources :messages
get 'threads/:id', to: 'message_threads#index'
post '/pusher/auth', to: 'pusher#auth'
I'm missing some required parameters, this is the error I get.
Pusher::Error - Bad request: Missing required parameter:
The flow according to this tutorial is that the message needs to be persisted first by the rails database before sending it to Pusher.
My question now is how do I produce the extra parameters (from_uid, thread_uid, to_uid) being used on the React side of the app here, to enable messages to be created?
Also, how do I authenticate the user using Pusher?
According to this Stack Overflow link they are getting from Rails the CSRF value like this - csrf = $('meta[name=csrf-token]').attr('content'). But I could not implement the same in React.
Answer from the author of the git repo.
The example I created here was pretty bare bones but below are a few bullet points that I hope will explain how you could expand on it.
Add a User model and Thread model to the Rails app
When a User is created, generate a public UID for the user (you can use Ruby's built-in SecureRandom.uuid to generate the id) and save that to the DB. This would become the ID for that user that you would expose in your javascript to allow for communications between users. All users would have a UID.
When a Thread is Created, generated a thread UID this would become the unique id for the conversation taking place
Add a Users_Threads has_and_belongs_to_many relationship so that you can eventually track the users that are subscribed to which threads
When React app loads, use an Ajax request to get a list of the current User's friends (returns list of user details + their uid) and a request to get all threads for current User
So let's say for example a User named Fred clicked on a User named Bob and wanted to send Bob a message but they do not currently have a thread. Fred types the message, clicks Submit and you send an Ajax request containing the message text, from_uid (Fred) and to_uid (Bob) with thread_uid as null (since there is no existing convo and no existing thread).
Your Rails app then receives that requests at a controller and sees that Fred is trying to send Bob a message and the thread ID is null, so the controller create a new thread (with its own UID) and then add two entries to users_threads one for the new thread_uid and bob's uid and another for the new thread_uid and Fred's uid. After that, you'd create a Message with that thread_uid and the participant details.
You'd also probably want users to see that they are part of a new thread without having to reload the page so you'd I think you'd want a Pusher channel subscription just for notifying users of a new thread. So I'd say in the UserThreads model after create a new thread you could send something like Pusher.trigger('threads_channel', user_secret_uid, { thread: new_thread_uid }). You'd also need to make sure in the react app that each user subscribes to the threads_channel for their user_secret_uid. For security, i'd make sure this is a different uid than the messaging otherwise people could subscribe to a list of a different users threads.

ahoy_meta gem don't work

Can anybody please help me how to create a event into ahoy_gem and how the visits will be tracked.
I already follow the documentation provided by the gem developer, but it can't help me how to properly use it.
Please help me.
Firstly check if it track visits,
head to rails console and run Visit.any? if it returns true then it is tracking the visits!
If it doesn't track visits, you can add the code below to application_controller.rb:
after_action :ahoy_track
protected
def ahoy_track
ahoy.track_visit
end
now it will track the visits.
In order to track events you have 2 options:
track events in the server side.
track events in the client side using js.
to track in the server side you should use the:
ahoy.track "Event name", properties: { one: "val", two: "val" }
This will create a record in the db with event named "Event name" with the properties one: "val", two: "val"
to track events in the client side using js:
ahoy.track("Event name", {one: "val"});
tracking in js won't create records in the db, but A POST request is sent to /ahoy/events with (from the documentation) and you will need to handle it there.
another thing: if you want to check events you can access them as Ahoy::Event or from a visit: visit.ahoy_events

Gibbon/Mailchimp API request to create interests inside interest-groupings

I'm using Gibbon, version 2.2.1 for Mailchimp, and I'd like to be able to create an interest inside an interest group. For instance, I have users who are subscribed to a class. My interest group is "Lessons", and an interest inside that interest group would be "Foo Lesson".
I'd like to be able to add the ability to add a new class in my site's CMS, which would make an API request on after_create.
class Lesson < ActiveRecord::Base
after_create :create_class_on_mailchimp
def create_class_on_mailchimp
require 'mailchimp_service'
mailchimp = MailchimpService.new(self)
response = mailchimp.create_class
self.interest_id = response.id
self.save
end
end
class MailchimpService
def initialize(lesson)
#lesson = lesson
#list_id = ENV['MAILCHIMP_LIST_ID']
end
def create_class
GB.lists(#list_id).interest_categories(ENV['MAILCHIMP_CLASSES_CATEGORY_ID']).interests.create(
body: {
name: 'foobar'
}
)
end
end
I keep getting this error:
Gibbon::MailChimpError:the server responded with status 404 #title="Resource Not Found",
#detail="The requested resource could not be found.",
#body={
"type" =>"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
"title" =>"Resource Not Found",
"status" =>404,
"detail" =>"The requested resource could not be found.",
"instance" =>""
},
#raw_body="{
\"type\": \"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/\",
\"title\":\"Resource Not Found\",
\"status\":404,
\"detail\":\"The requested resource could not be found.\",
\"instance\":\"\"
}",
#status_code=404
What this tells me is that I'm not using the correct resource name? There doesn't seem to be any documentation for this kind of request in Gibbon's limited docs, nor does it seem to be something that Mailchimp goes over. Here is a link to Mailchimp's docs that goes over the requests for interests inside interest-groupings, however, there doesn't seem to be a create option... Just read, edit, and delete. This seems silly to me, as I can imagine people would want to create interests from somewhere other than Mailchimp's dashboard.
I've tried using name, title, and interest_name for the resource name, but none work. I've also tried using REST API calls, but I receive the same response.
Am I doing something wrong, or is this really something that Mailchimp doesn't offer? It'd be a huge bummer if so, since I'll be creating many classes that I want people to be able to subscribe to, and it would be a major pain to have to do this all manually.
I'm pretty sure POST works to create interests, although it does appear to be missing from the documentation. What is probably happening is that either your list ID or interest category ID is incorrect. You might want to try using the API Playground to track down the exact IDs for both of those entities.

GETing different Active Resource models in a single request

Is it possible to receive objects of different Active Resource models in a single request? For example the request "GET /user/joe/articles/1.xml HTTP/1.1" returns an object from User ("joe") and another object from Article (id "1") from the server.
I know it is possible to send these objects inside an array to the client, but ARes can't process them. Is there anyway to "break" the response in two and send the results to different Active Resource models for processing?
I think what you want to do is include the user xml inside the article xml that is returned.
If you are using ActiveRecord to make the xml the you can use the :include parameter of the to xml call to get the user included in the article response. The output is something like
<article>
...
<user>
...
</user>
</article>
You should then be able to call '.user' on the returned article object to get at the user properties.

Resources