Get Entities through common relation - nestjs

I have three entities - Company, User, Accessgroup. I need to restrict User access to Companies with Accessgroups.
Users and Accessgroups is many to many, Companies and Accessgroups is many to many.
For example, User 'Murphy' have relation with Accessgroups 'Detroit' and 'Delta-City, and Company 'OCP' have relation with Accessgroup 'Detroit'.
How can I pull Companies that have same Accessgroups with User?

I found the solution, one should use TypeORM Query Builder.
First of all, get accessgroups Ids that user have relation with.
For example, let's said that user Murphy included in accessgroups with agIds: [1, 2].
Then we could get all Companies that user have access with this query:
this.companiesRepository.createQueryBuilder('company')
.innerJoin('company.accessgroups', 'accessgroup', 'accessgroup.id IN (:...ids)', {ids: agIds})
.getMany();

Related

How to sort posts list based on different sorting criteria in mongodb?

I am building social network app. i have used node.js(express) and mongodb as backend.
Now, i want to list all posts based on different sorting critrea.
user can set his business category(optional). i have store each user current location in user documents.
Now, i need the post list of all user including friends based on below sorting criteria
post from near by friends (lowest distance first)
post from user who has same category as logged in user if logged in user has set business category
post from user who has different business category
post from user who has not set business category (latest first(created at desc))
how should i build structure for friend system in order to achieve above sorting posts?
Any help would be appreciated.
Thanks
First you should pay attention to the memory - this structure grows with N^2 speed where N - posts count.
This task seems to be quiet complex so I suggest to add some logic on backend.
For example, look at CQRS+ES (I think it's suitable technology in your case). So, posts are events and you would have sorted list for every user on read side. And you would update all these structures on every post. (You can read about CQRS here http://cqrs.nu/ and check simple CQRS framework with express support here https://reimagined.github.io/resolve )
In addition I recomend you to limit posts count for every users' list.

Data Modeling for special e-commerce Site in Aerospike

Hello I have just started using Aerospike so I need some details what will be the good data model for my e-commerce platform, I am not able to design a data model for this in Aerospike which can work perfectly.
Here are some basic requirements for my e-commerce platform:
1.>User Set(For login & register an basic information of user)
2.>Product Set (For storing product info like name and image and options and color options etc)
3.>Order Set ( To track record of the user order )
The complex requirement for the special Set required for database is as follows:
1.For each product that a user will buy a Share Code will be generated which a user can share with his/her friends and family to get benefits for future.
2.The user who buys a product with somebody’s Share code, then the details that this user bought the “xyz” product must be transferred to the owner of the share code and also a Share code for this user will also be generate which he/she can share with his/her friend.
3.And also the user must be able to know how many persons shared his/her code an also the user’s who buys a product from the share code of the 1st level user’s Share Code.
So I want to keep record of the users 2 level below the current user.
Looks like you are trying to model a Multi-Level-Marketing order management system.
User and Product are straight forward. Records TTL = live for ever, never delete for all sets. user_info, product_info may be updated.
For simplicity, each product purchase is just one product_id.
By PK I mean the primary key of the record.
User Set: {PK:user_id, user_info:{.....}}
Product Set: {PK:product_id, product_info:{.....}}
Order Set: {
PK: order_id,
order_details:{buyer:user_id,
item:product_id, qty:...,
share_code: "order_id:xyz",
parent_code:"parent_order_id:abc" },
level1_orders:[List of order_ids],
level2_orders:[List of order_ids]
}
The share_code is composite string comprising the order_id:some_code.
For first set of orders (level 0 orders) parent_code may be zero.
Consider order_id = 213, share code: "213:abc", parent_code:"112:pqr"
If any user purchases using share code "213:abc", and this level 1 purchase order_id is 310, make entry in Order set for {PK:310, ... share_code:"310:cde", parent_code:"213:abc", ....} then update order_id=213 - to its level1_orders list, append 310 order_id.If order 213 has a parent_code, in this case it is "112:pqr" also update order_id 112 in its Level2 order lists by appending this order_id, 310 to it.
Now you have all the info you need for your model.
Note: This is a multi-record update model. Be mindful of potential inconsistencies if client fails midway or other bad things happen. There are advanced techniques to address that situation.
However, this may be a good starting point for you. Let me know if this was helpful. If it is different than what you wanted (your part 3 was not very clear to me), you may have to modify the model but the technique highlighted may be helpful.

Duplicate Detection In CRM

In my organization, I have a sales department. The users in the sales department have given leads. Daily They call to the different leads. Now I want that two Sales Persons are not calling the same lead. So how to prevent this situation in the CRM. Moreover we are giving the random leads to all the Sales Persons. There is a possibility that two or more sales persons have same leads.
The Privileges given to the Sales Persons are:
They are not able to see the leads of each other.
They are not able to see the accounts of each other.
They are not able to see the contacts of each other.
Now I want that two or more Sales Persons are not having the same leads so they are not calling the same person. So how to prevent this situation in the CRM?
The duplicate detection is limited to the records for which you are granted access. See Point 4 in this article: http://blogs.msdn.com/b/crm/archive/2008/04/29/duplicate-detection-security-model.aspx
All users with Read privilege on the base and duplicate records and Read privilege on System Job entity can view the duplicates. Every user will view the duplicates according to his access level on that entity. For example, if Tim has Basic read access on Accounts entity and Jack has Global read access, then for a duplicate detection job ran by Tim for all the account records in the system, Tim will see the duplicate account records that he owns but Jack will see duplicate account records, created by all users in the organization and detected in that run.
So you have to either run a duplicated detection with administrative rights peridically or you have to grant more rights.
The reason for this is pretty simple: how would you show a user that there is a possible duplicate record when he is not allowed to view it. So it will be handled like the records don't exist.
At last I have found the answer of the above question.
I have increased the privileges given to the sales persons. I have given access to see all the records of all the sales persons.
But In the Front End, I hide the system views like "All Leads", "Active Accounts", "Active Contacts" etc. and made custom views for the sales persons. Edit the filters of each view.
Because These are the views which able the sales persons to see all the records. So I hide these views from the sales persons and make custom views.
By this the two or more sales persons are not having the same leads.So they are not calling the same leads.
I will explain you how it helps: Suppose One sales person import the leads and after some time the other sales person also import some leads and if some leads are same, then the CRM will not import the duplicate leads and import the other leads which are not duplicate of the other.Moreover the sales person can see their leads which are not imported in their system by going in the Workplace and then to Imports.
Also I have made the custom Duplicate Detection Rules according to my requirement. These rules will check that the leads are not duplicate of each other according to my requirement.
I think you will need to increase the security privileges of each user, so they can see each others records. Or have an admin account for importing records.

Allowing Users to create custom groups of Countries

I'm working on a project for a customer, and one of the requirements is that Users should be allow to assign to each Product (in their case, a Node) a Country or a Region, where the Region is simply a group of Countries, not necessarily in the same area.
I've seen there are many different ways to manage a list of Countries, often suggesting to use Taxonomy for them, but I can't figure out how could I allow users to create these "Regions". To make things complicated, customer wants to have a simple interface, where only one field is present on the form. In this field, Users must be able to choose either a Country or a Region.
Perhaps I could implement everything using Nodes, i.e.:
- Country Nodes
- Region Nodes, with a multiple-valued Node Reference to Country Nodes
But I wonder if that would not be too heavy...
I hope the issue is clear, if not feel free to ask and I'll try to explain it better. Thanks for all suggestions.
I ended up creating my own tables and code to handle the whole thing, as I couldn't find any better solution. I used tables from IP2Country module as a source for Country Codes.

How to determine the aggregate root

I have an application in which an Engineer accesses gas wells. He can see a list of wells by choosing any combination of 7 characteristics. The characteristics are company, state, county, basin, branch, field, operator in their respective order. The application starts and I need to retrieve a list of companies. The companies the user sees is based on their security credentials. What would be my aggregate root/domain object which to base my repository. I first thought user, but I never retrieve anything about a user. The combination of those items and a couple of other attributes are collectively called wellheader information. Would that be the aggregate root or domain object for my repository?
Thanks in advance
With a short description like that, it can only be a quess on how your design could be.
As I read it, your are really interested in wells for a given engineer. (is the engineer the user you mention?)
So a first try could be to model the concept of a well as an aggregate root.
So maybe something like this:
ICollection<Well> wells = WellRepository.GetWellsForEngineer(engineerInstance);
Maybe your engineer is associated with a characteristics object.
Either way, you have to associate the engineer with wells in a given company, state and so on to be able to extract which wells the engineer is actualy assigned to.
If this dosen't help you, maybe you could elaborate on your domain.

Resources