How to return result that belongs to user and restrict other data being sent in the result in FeathersJS? - feathersjs

I am new to feathersjs and I've breaking my head on how to restrict users from viewing other's data. For instance from postman when you do find/get(id) http://localhost/users you see list of all the users that are currently registered. However I would only like to return the current users data.
From:
{
"total": 2,
"limit": 10,
"skip": 0,
"data": [
{
"id": 1,
"email": "test#feathersjs.com",
"auth0Id": null,
"createdAt": "2020-02-01T23:16:30.833Z",
"updatedAt": "2020-02-01T23:16:30.833Z"
},
{
"id": 2,
"email": "test1#feathersjs.com",
"auth0Id": null,
"createdAt": "2020-02-01T23:31:50.904Z",
"updatedAt": "2020-02-01T23:31:50.904Z"
}
]
}
To:
{
"total": 1,
"limit": 10,
"skip": 0,
"data": [
{
"id": 1,
"email": "test#feathersjs.com",
"auth0Id": null,
"createdAt": "2020-02-01T23:16:30.833Z",
"updatedAt": "2020-02-01T23:16:30.833Z"
},
]
}
How do I filter result in the after hook function? Can someone please explain.
Answer:
Missed a basic step. All the find(), update() etc return result based on parent class. By Customizing find() method in your own class and using Sequlize.findOne({}) you can return the result you desire.
Damn! I was stupid to not read the docs fully.

Related

How to multiple update a nested array within document at couchbase?

I have a bucket called chat which includes document model like
{
"id": "50542400778551298_5001_abcdef",
"customer": {
...
},
"seller": {
...
},
"complaints": [],
"messages": [
{
"id": "2828911d-b96f-4b90-a52c-252d0324ab69",
"owner": "SELLER",
"content": "Merhabalar",
"readingDate": null,
"createdAt": "2022-06-27T09:16:42.49158+03:00"
},
{
"id": "8cd8c4b7-d599-4dfa-9fa7-be304a3b72b9",
"owner": "SELLER",
"content": "İlan hakkında detay bilgi verir misin?",
"readingDate": null,
"createdAt": "2022-06-27T09:17:43.329741+03:00"
},
{
"id": "4f0ae912-7d65-4e79-9058-2b048c4d9845",
"owner": "SELLER",
"content": "Huhuu",
"readingDate": null,
"createdDate": "2022-06-27T09:18:37.491531+03:00"
},
{
"id": "d2dd61b3-7ff1-4139-a411-9a917c00d4f3",
"owner": "SELLER",
"content": "Orada kimse var mı?",
"readingDate": null,
"createdAt": "2022-06-27T09:18:45.917564+03:00"
},
{
"id": "ea2d8dca-2f8c-4987-9c07-7515a6a0063f",
"owner": "SELLER",
"content": "Döner misininiz?",
"readingDate": null,
"createdAt": "2022-06-27T09:18:52.80337+03:00"
}
],
"status": "ACTIVE"
}
I want to update all messages readingDate a specific time like (2022-06-27T09:18:52.80337+03:00)
But I cannot find an easy way to accomplish this task.
I tried to write an N1QL like
UPDATE c.messages SET c.messages.readingDate = NOW_LOCAL()
FROM chat AS c
WHERE META().id = '50542400778551298_5001_abcdef';
but it is not working and gives error
[
{
"code": 3000,
"msg": "syntax error - at SET",
"query": "UPDATE c.messages SET c.messages.readingDate = NOW_LOCAL()\nFROM chat AS c \nWHERE META().id = '50542400778551298_5001_abcdef';"
}
]
I saw https://docs.couchbase.com/server/current/guides/bulk-operations.html.
As far as to my understanding, the general solution gets all messages and updates them based on cas.
But maybe I believe there is an easy way so I wanted to ask the StackOverflow community.
Thanks to this question I wrote n1ql like
UPDATE chat USE KEYS '50542400778551298_5001_abcdef'
SET c.readAt = NOW_LOCAL() FOR c IN messages END
RETURNING messages;

Microsoft Power-Automate Select operation does not work as intended

I've got a Select-Operation on an an object that contains one key more than once.
It's practically two versions of one object in one JSON object.
I want to get the id of both of those Objects.
When i inspect the Object, i can clearly see the two different Id's, but the Select-Operation returns only one of them twice.
This is the original Object:
[
{
"Created": "2020-06-05T11:47:42",
"ID": 9,
},
{
"Created": "2020-06-05T11:06:04",
"ID": 10,
}
]
The Select-Operation looks like this:
{
"inputs": {
"from": "#body('Rest')?['value']",
"select": {
"ID": "#triggerBody()?['ID']",
"Created": "#triggerBody()?['Created']"
}
}
}
And it returns:
[
{
"Created": "2020-06-05T11:47:42",
"ID": 9,
},
{
"Created": "2020-06-05T11:47:42",
"ID": 9,
}
]
I don't really understand what's going on.
"select": {
"ID": "#triggerBody()?['ID']",
"Created": "#triggerBody()?['Created']"
}
is wrong, it should select item()?['ID']

BlockCypher Send Transaction

I am trying to implement the BlockCypher Ethereum API following the docs here:
https://www.blockcypher.com/dev/ethereum/#transaction-api
I am using the Test Network /beth/test, rather than /eth/main.
I successfully created 2 addresses and called the faucet to fill one address with some 1000000000000000000 wei.
But then I can’t seem to be able to send a valid transaction…
Here are all (test) info:
ADDRESS 1
{
"private": "42eb005cbebd33f55b821864fcdc0755876317345ac9d8ad2bf94d734f78395f",
"public": "04c7238abb2e99c50ef3e4fabe21dbf2701f9dc60de24b40e743e84745984957ba7059a3e0b38c226b3580a95ed0e154fba407d089f5c02f90aa5797ead7fc190e",
"address": "7a9373010ce86d8fcd7dd46588d351cddabe7ce8"
}
ADDRESS 2
{
"private": "4a62456df43b89244d349948008064cfe1279ef3a632823f30b371ac54f879a1",
"public": "046c01b338e3c5224aa70fa302fefa5c9767d0f79aa16327e703d01924e894809460b22ab52974706543682d8e16063fd6cbd302b052762a3be5d7720efc4d2fa9",
"address": "73befec27510c64c303d82191eaf276a94603090"
}
Address 2 contains 1000000000000000000 wei, and I am trying to send 1200000000000000 wei to Address 1.
I use the New Transaction method, I send this:
{"inputs":[{"addresses": ["73befec27510c64c303d82191eaf276a94603090"]}],"outputs":[{"addresses": ["7a9373010ce86d8fcd7dd46588d351cddabe7ce8"], "value": 1200000000000000}]}
And I get this response:
{
"tx": {
"block_height": -1,
"block_index": 0,
"hash": "4e875a673e59f0b2d16123659b3a72a8d818a7cb832c5ed1a4bbf8f5b0a4ee87",
"addresses": [
"73befec27510c64c303d82191eaf276a94603090"
],
"total": 1200000000000000,
"fees": 21000000000000,
"size": 43,
"gas_limit": 21000,
"gas_price": 1000000000,
"received": "2018-05-27T13:51:23.247159854Z",
"ver": 0,
"double_spend": false,
"vin_sz": 1,
"vout_sz": 1,
"inputs": [
{
"sequence": 0,
"addresses": [
"73befec27510c64c303d82191eaf276a94603090"
]
}
],
"outputs": [
{
"value": 1200000000000000,
"addresses": [
"7a9373010ce86d8fcd7dd46588d351cddabe7ce8"
]
}
]
},
"tosign": [
"8b765bd6c2065f612eb00aa55c50759049779cff50151395aefc5a3dafa5cc10"
]
}
Then I use Signer with the ToSign from the New Transaction response, and a Hex-Encoded Private Key of Address 2:
signer 8b765bd6c2065f612eb00aa55c50759049779cff50151395aefc5a3dafa5cc10 34613632343536646634336238393234346433343939343830303830363463666531323739656633613633323832336633306233373161633534663837396131
I get this signature:
30440220644db87755b0a8620e7e52bd64977c79350c0989afd587090be13a8f76efbf3a0220463eb6351c839b086f5874f6a125ec4723fb30eb26a6a831600cb4d3034cd9d3
So then I try the Send Transaction method of BlockCypher, here’s my request:
{
"tx": {
"block_height": -1,
"block_index": 0,
"hash": "4e875a673e59f0b2d16123659b3a72a8d818a7cb832c5ed1a4bbf8f5b0a4ee87",
"addresses": [
"73befec27510c64c303d82191eaf276a94603090"
],
"total": 1200000000000000,
"fees": 21000000000000,
"size": 43,
"gas_limit": 21000,
"gas_price": 1000000000,
"received": "2018-05-27T13:51:23.247159854Z",
"ver": 0,
"double_spend": false,
"vin_sz": 1,
"vout_sz": 1,
"inputs": [
{
"sequence": 0,
"addresses": [
"73befec27510c64c303d82191eaf276a94603090"
]
}
],
"outputs": [
{
"value": 1200000000000000,
"addresses": [
"7a9373010ce86d8fcd7dd46588d351cddabe7ce8"
]
}
]
},
"tosign": [
"8b765bd6c2065f612eb00aa55c50759049779cff50151395aefc5a3dafa5cc10"
],
"pubkeys": ["046c01b338e3c5224aa70fa302fefa5c9767d0f79aa16327e703d01924e894809460b22ab52974706543682d8e16063fd6cbd302b052762a3be5d7720efc4d2fa9"],
"signatures": ["30440220644db87755b0a8620e7e52bd64977c79350c0989afd587090be13a8f76efbf3a0220463eb6351c839b086f5874f6a125ec4723fb30eb26a6a831600cb4d3034cd9d3"]
}
And I get back this error:
"Address 0xF6A14d976c86bDbe050cA9426aD491d750be33B1 computed from signature differs from provided one 73befec27510c64c303d82191eaf276a94603090."
Any idea of what am I doing wrong?
I tried several things already, read a few similar issues online, but still no luck.
I also tried the Raw Transaction method of BlockCypher, and also no luck there..
I am not clear what it means as “raw hex-encoded transaction”, I tried to send the Hex-Encoded input I sent to New Transaction:
{"inputs":[{"addresses": ["73befec27510c64c303d82191eaf276a94603090"]}],"outputs":[{"addresses": ["7a9373010ce86d8fcd7dd46588d351cddabe7ce8"], "value": 1200000000000000}]}
So I sent to https://api.blockcypher.com/v1/beth/test/txs/push?token=[mytoken] this:
{"tx":"22696E70757473223A5B7B22616464726573736573223A205B2237336265666563323735313063363463333033643832313931656166323736613934363033303930225D7D5D2C226F757470757473223A5B7B22616464726573736573223A205B2237613933373330313063653836643866636437646434363538386433353163646461626537636538225D2C202276616C7565223A20313230303030303030303030303030307D5D"}
But I get the error:
“rlp: expected input list for types.TxData”.
Please, any help is very much appreciated!
The private key you're passing to the signer tool is not correct. As Address 2 is the sender in your example, you should be using its private key instead, which is 4a62456df43b89244d349948008064cfe1279ef3a632823f30b371ac54f879a1.

Fetch json array in PostgreSQL procedure

I'm using PgSQL9.6.
I have procedure named as createBooking in PgSQL. It receive few parameters and one of them is additional type text.
Request is
{
"email": "user2#name.com",
"start_date": "2016-10-21 11:00:00+00",
"end_date": "2016-10-21 15:00:00+00",
"guests": 50,
"firstname": "User",
"lastname": "Name",
"payment_type": "Method 1",
"phone": "+871253875123",
"user_id": 22,
"y_id": 3,
"additionals": [
{
"extrasId": 6,
"money": 19750,
"packageId": null,
"amount": 3
},
{
"extrasId": 11,
"money": 64380,
"packageId": null,
"amount": 5
},
{
"extrasId": null,
"money": null,
"packageId": 3,
"amount": 1
}
]
}
I'm trying fetch 'additioanl'.
FOR i IN SELECT json_array_elements(createBooking.additional)
LOOP
sum := sum + i->>'money';
END LOOP;
It does not mater what is in body of loop.
When I calling this function via PostMan it is sending me response with error.
{
"hint": "No function matches the given name and argument types. You might need to add explicit type casts.",
"details": null,
"code": "42883",
"message": "function json_array_elements(text) does not exist"
}
Can some body explain me how should I fetch it?
Thank you.

ember data custom serializer for a json

I have a Session object I want to model in Ember data. (actually display sessions but it's the same)
The JSON from the server looks like this (cannot be changed):
{
"metadata": {
"page": 1,
"page_size": 100,
"total_num_objects": 7,
"total_num_pages": 1
},
"result": [
{
"api_path": "/rest/sessions/2",
"end_time": 1412687629.42063,
"hostname": "127.0.0.1",
"id": 2,
"logical_id": "c6656738-4e23-11e4-9017-685b35b63131_0",
"product_name": null,
"product_revision": null,
"product_version": null,
"start_time": 1412687629.26851,
"status": "SUCCESS",
"type": "session",
"user_name": null
},
{
"api_path": "/rest/sessions/3",
"end_time": 1412688377.15329,
"hostname": "127.0.0.1",
"id": 3,
"logical_id": "84707366-4e25-11e4-a659-685b35b63131_0",
"product_name": null,
"product_revision": null,
"product_version": null,
"start_time": 1412688377.11507,
"status": "SUCCESS",
"type": "session",
"user_name": null
},
...
I realize I need to write a custom RESTSerializer but I can't figure out what do I need to do in order go get rid of metadata + make ember realize that result is actually a session.
Side question:
Can I make the DS.Model.extend attributes like what I get from the API or do I must use CamelCase and use normalizeHash
You'd probably need to do something like this:
App.SessionSerializer = DS.RESTSerializer.extend({
normalizePayload: function(payload) {
return {
sessions: payload.result
};
}
});
See: http://emberjs.com/api/data/classes/DS.RESTSerializer.html#method_normalizePayload
Regarding your other issue, look at DS.ActiveModelAdapter / ActiveModelSerializer. ActiveModelSerializer handles the underscore convention in the JSON.