Recursive updating of Mongoose document - node.js

I am currently trying to implement symmetrical (AES256-GCM) encryption to a document before it gets saved into the database. Therefore I have hooked into the pre('save') method to grab data and encrypt it before it gets saved to the database. I need to do this to meet regulations which require all medical data to be encrypted and can only be decrypted by the user who owns the data. This is was for a bit of background
So I have a complex schema setup for the user which contains a lot of PII information so I can't post an example but I can give you a sanitised JSON structure of the data, that doesn't give away what I am working on. (NDAs...).
{
"title": "",
"firstName": "",
"lastName": "",
"XXXXXXXXXX": [{
"XXX": "",
"XXX": "",
"XXX": "",
"XXX": "",
"XXX": "",
"XXX": "",
"XXX": "",
"XXX": ""
}],
"contact": {
"contactNumbers": [{
"type": "",
"number": ""
}],
"emailAddresses": [{
"type": "",
"address": ""
}]
},
"XXX": {
"XXX": {
"XXX": "",
"firstName": "",
"lastName": "",
"XXX": ""
},
"XXX": [{
"XXX": "",
"firstName": "",
"lastName": "",
"XXX": ""
}],
"name": "",
"XXX": "",
"XXX": "",
"XXX": ""
},
"XXX": [{
"name": "",
"number": ""
}],
"XXX": [{
"code": "",
"name": "",
"XXX": "",
"XXX": ""
}],
"XXX": [{
"code": "",
"name": "",
"XXX": "",
"XXX": ""
}],
"XXX": [{
"name": "",
"XXX": "",
"contactNumber": "",
"emailAddress": "",
"address": {
"street1": "",
"street2": "",
"suburb": "",
"city": "",
"province": "",
"country": "",
"postalCode": ""
}
}]
}
So I have been trying to recursively loop through the data so that I can encrypt each field before saving the document. I came across the following package which somewhat works. It doesn't recurse into the arrays. I tried converting the document into a JSON Object to make it easier to work with. Which didn't help much besides removing the metadata from the document object.
I was trying to write a cursive function (C# is my wheelhouse) but I cannot access the length of the children of a JSON object so I couldn't recurse on that.
I would greatly appreciate it if someone has a solution to recursing through a Mongoose document, because I am stumped.

Okay, so that package I mentioned did the trick. There is an unspecified option that treats arrays as objects which allows the iterator to recurse into the arrays. So the working iterator looks as follows:
let iterator = require('object-recursive-iterator');
iterator.forAll(jsonProfile, (path, key, obj) => {
obj[key] = 'processed';
}, {treatArrayAsObject: true});
Hope this helps someone in the future.

Related

cosmos cannot delete the duplicate uuid

Hello I post this little message to know if it is possible to delete
The model is as below:
Create Log data
{
"publisherID": "",
"managerID": "",
"mediaID": "",
"type": "",
"ip": "",
"userAgent": "",
"playerRes": "",
"title": "",
"playerName": "",
"videoTimeCode": 0,
"geo": {
"country": "",
"region": "",
"city": "",
"ll": []
},
"date": "",
"uuid": "",
"id": ""
}
Select all log data: SELECT * FROM a
[
{
"publisherID": "{ID}",
"mediaID": "{ID}",
"type": "Load",
"ip": "67.69.69.41",
"userAgent": "Mozilla/5.0 (Android 11; Mobile; rv:91.0) Gecko/91.0 Firefox/91.0",
"parentReferrer": "link.com",
"title": "title here",
"playerName": "page",
"videoTimeCode": 0,
"geo": {
"country": "CA",
"region": "Ontario",
"city": "Guelph",
"ll": [
43.5588,
-80.3004
]
}
}
]
[SQL-QUERY]: 01 - does not work because there are always duplicate uuids
I want to have all the requests of type progress type: 'Progress'
Organize progress type data by minute according to the date
SELECT COUNT(c.uuid) as total,
left(c.date,16) as time
FROM c
WHERE c.mediaID = '{ID}'
AND (c.date BETWEEN '2021-08-02T14:48:00.000Z' AND '2021-09-03T14:48:00.000Z')
GROUP BY c.uuid, left(c.date, 16)
[
{
"total": 9,
"time": "2021-09-03T14:07"
},
{
"total": 40,
"time": "2021-09-02T12:51"
},
{
"total": 51,
"time": "2021-09-02T12:50"
}
]
[SQL-QUERY]: 02 - cosmos error !
Syntax error, incorrect syntax near 'DISTINCT'.
SELECT COUNT(DISTINCT c.uuid) as total, left(c.date,16) as time
FROM c
WHERE c.mediaID = 'ckpwphqbj10852aav7ib0713o8'
AND (c.date BETWEEN '2021-08-02T14:48:00.000Z' AND '2021-09-03T14:48:00.000Z')
GROUP BY c.uuid, left(c.date, 16)
I want to delete duplicate uuids and then group unique uuids by date
are you looking for this?
counting distinct uuid:
SELECT COUNT(distinct c.uuid) as total, left(c.date,16) as time
FROM c
WHERE c.postID = '155478'
AND (c.date BETWEEN '2021-08-02T14:48:00.000Z' AND '2021-09-03T14:48:00.000Z')
GROUP BY left(c.date, 16)

can we modifiy the transaction record in hyperledger composer

I have a transacton called updatewarranty.In that updatewarranty transaction i am updating a asset called warranty.
This is my json
{
"$class": "org.network.warranty.Transfer",
"TransferId": "9427",
"AuthKey": "",
"TransferDate": "2018-06-30T05:50:32.767Z",
"customer": {
"$class": "org.network.warranty.Customer",
"CustomerId": "2599",
"Address1": "",
"Address2": "",
"Authkey": "",
"City": "",
"Country": "",
"Email": "",
"Mobile": "",
"State": "",
"UserType": 0
},
"retailer": {
"$class": "org.network.warranty.Retailer",
"RetailerId": "8389",
"Address1": "",
"Address2": "",
"Authkey": "",
"City": "",
"Country": "",
"Email": "",
"Mobile": "",
"State": "",
"UserType": 0
},
"warranty": {
"$class": "org.network.warranty.Warranty",
"WarrentyId": "0766",
"End_Date": "2018-06-30T05:50:32.767Z",
"Start_Date": "2018-06-30T05:50:32.767Z",
"IS_Internationaly_Valid": "",
"Item_QRCode": ""
}
}
I have a transaction named getwarranty which takes the warranty id as input.
this is my json
{
"$class": "org.network.warranty.getWarranty",
"warranty": "resource:org.network.warranty.Warranty#0766"
}
When i see the transaction record for getwarranty i dont have the entire transfer record. I have only this information
{
"$class": "org.network.warranty.getWarranty",
"warranty": "resource:org.network.warranty.Warranty#0766",
"transactionId": "6e35c9cb-d3a6-41d8-8c95-fa22c7681824",
"timestamp": "2018-06-30T05:50:54.851Z"
}
how can i get the warranty asset?

python runs slow while executing below piece of code

tag_api=”htpps://url....”
json_data=requests.get(tag_api)
length=len(json_data[‘tags’])
line=0
while length>=2:
if line<=lenght-1:
with open(‘file.txt’,’a’) as fp:
fp.write(json_data[‘tags’][line][‘name’])
line=line+1
While I run this code it gets slow at while loop. Do you suggest what could be the issue?
{ {
{ "id": "",
"category": "",
"type": "",
"tags": { "id": "", "name": "" }
{ "id": "", "name": "" } },
{ "id": "",
"category": "",
"type": "",
"tags": { "id": "", "name": "" } },
{ "id": "",
"category": "",
"type": "",
"tags": { "id": "", "name": "" } },
{ "id": "",
"category": "",
"type": "",
"tags": { "id": "", "name": "" }
{ "id": "", "name": "" } },
} }
I am trying to get the tags that have more than two index and write to a file
Opening a file (and closing it) are quite slow operations. Since it's the same file every time, you should move the opening out as far as possible.

Replace object in array in Mongoose

I'd like to replace an object in an array using an index, but nothing will save. This is what the document looks like:
{
"_id": {
"$oid": "58a71ec0c80a9a0436ae2fb1"
},
"owner": "contact1#gmail.com",
"contacts": [
{
"work": "",
"home": "",
"mobile": "",
"email": "",
"company": "",
"last": "Contact",
"middle": "",
"first": "New"
},
{
"first": "Another",
"middle": "",
"last": "Contact",
"company": "",
"email": "",
"mobile": "",
"home": "",
"work": ""
}
],
"__v": 1
}
And this is what I've tried..
Contacts.findById({_id: "58a71ec0c80a9a0436ae2fb1"}, function(err,document) {
document.contacts[req.body.indexOfObjectToBeEdited] = req.body.updatedObject
console.log(document)
document.save(function(err) {
return res.json({event:"Updated Contact"})
})
})
Right before document.save() I console.log(document) and it reflects the correct changes. But when I save, nothing is updated in the mongodb and I receive no errors. What should I be doing differently?
try inserting this line right before saving. As modifying an array require we need to manual tell the mongoose the it is modified.
document.markModified("contacts");
Check the Usage Notes in the documentation for more information
http://mongoosejs.com/docs/schematypes.html

Structured data for company with two (or more) branches

I'm trying to figure out how to implement structured data for my company with 2 branches. The branches are in the same city, but there is not head offfice or a difference in hierarchy. The branches do not have their own landing page on the website. It's a dentist office so I wanted to use LocalBusiness -> Dentist.
Because I found that nesting the data was better then to separate the data, I thought of using #brancheOf or #subOrganization. However when I try this the testing tool tells me these properties do not work with #type 'Dentist'. Can somebody give me some advise on how to continue? This is what I had so far:
<script type='application/ld+json'>
{
"#context": "http://www.schema.org",
"#type": "Dentist",
"name": "",
"url": "",
"sameAs": [
""
],
"logo": "",
"image": "",
"description": "",
"address": {
"#type": "PostalAddress",
"streetAddress": "",
"addressLocality": "",
"addressRegion": "",
"postalCode": "",
"addressCountry": ""
},
"geo": {
"#type": "",
"latitude": "",
"longitude": ""
},
"hasMap": "",
"openingHours": "Mo 09:00-17:30 Tu 08:30-17:30 We, Th, Fr 08:00-17:30",
"contactPoint": {
"#type": "",
"contactType": "",
"telephone": "",
"email": ""
},
"#subarganization": "http://www.schema.org",
"#type": "Dentist" (I didn't continue here because I got the error, but I would enter address details etc. from the second branch).
}
</script>

Resources