Assign ID that increments automatically - MongoDB - javascript

I'm currently working on a ticketing system using Mongo, I've got the basics down, (making tickets, admins responding, etc) but I want to assign a unique ID to allow the user to view the ticket, so /tickets/view/(id), problem is, I don't really know how to assign one, I don't want to use the auto assigned mongo one because it just looks bad on the system, so, how would I assign a ticket with an id that goes up each ticket?
Using: MongoDB Node Module, Express

You can create separate counters collection for sequences, which will store current values.
Then on any insert - query for current value for wanted counter and increment in single operation.
Complete tutorial on that: MongoDB Auto-increment sequence

Related

How to limit array size in supabase?

I am making an application which involves users storing their favorite content series (which each have a unique ID).
The problem is : I want to check if those IDs exists in the database and also to limit the size of this list to a floor (~100 items).
So far, I have tried to implement a RLS policy but checking the content of the posted data is too complex especially for arrays.
I am considering the use of a sort of gateway API that makes a bridge between the user and the database of their profile to insure more checks but before working on that I want to be sure there is no way to implement this by relying on Supabase only.
You can use a database constraint to limit the size of the list to a floor of 100 items. Additionally, you can use a SQL query to check if the IDs exist in the database.
To limit the size of the list, you can use a check constraint on the table that stores the user's favorite content series. The check constraint can be defined to ensure that the size of the array is not greater than 100.
To check if the IDs exist in the database, you can use a SQL query with the "IN" operator. The query can be written to check if each ID in the user's list exists in the database.
These solutions can be implemented using Supabase only, without the need for a gateway API.

Socket.emit triggers a new connection

I'm using socket.io in my app, and attaching a socket id to each user after login. I get that info on front end and when I'm trying to emit an event, it creates a new connection so every user gets a new socket id, but the old ids are stored on front end and I'm sending the data to that old ids. What is the solution? (I'm using Angular)
I've tried creating groups, but it really slows down the app because I have to create as many socket groups as every user's friends.
My suggestion to you to solve this problem is to use a unique ID for each user. This unique ID can be created and delivered to the user through rest api. The user puts his ID in an object before every connection to the socket. Your task is this. At the same stage of user connection, create an object for each user. In this object, there are two parameters. The first one is the user ID that does not change, and the second one is the user ID that changes. In this way, you have a list of objects that are inside Each object has two mentioned parameters, it is enough to compare the user using the socket ID loop and implement your logic and operations. Note, at the end of connecting the user, remove the user from the list.

How to show records *only* from current user php

I'm developing a web app that allows me to generate product orders in PHP. When I first started, I was thinking only about my product orders, however, I have 3 more sales representatives and I would like for them to use it as well.
The problem I'm currently facing is that whenever I'm looking for product orders (using an ajax), all users are allowed to see all the product orders. I would like that each user can only see **their* product orders, but I'm not sure how the query should be.
What would the query be considering these tables?
Thanks in advance!
UPDATE:
So, thanks to #Nicolas answer, I was able to get only information for a selected vendor. However, the 'nombre_cliente(client name)' from 'clientes' tables is wrong for every row. It is showing only the first row. Other information is correct, except for the name. Here is what the results look like now, and how I'm looping through the data.
results
looping
Thanks in advance to everyone.
From what i understand, You would need to know who's asking the server for every request. There's multiple way to achieve this and we don't have enough informations about your infrastructure to give you a good way to accomplish this. Are you using PHP session, are you using an API with API keys, etc. Either way, To get only the product order for a particular vendor, you would need to execute a SQL that would look like that :
SELECT * FROM `facturas` WHERE `id_vendedor` = 1;
In this example, 1 is the id of the connected user.
This request will filter every order and returns only the one that have been done by the user with the id : 1.
You could also join both tables together and filter by some other property of the user table. Lets say we want to get every order by the user whose name contains "john". we would do a request like that :
SELECT facturas.* FROM `facturas` JOIN `users` ON `facturas`.`id_vendedor` = `users`. `user_id` WHERE `users`.`firstname` LIKE '%john%';
In this case, we are joining both table, mapping every id_vendedor to an user_id in the user table, and filtering by firstname. The like operator can be translate to : 'anything john anything'.
IMPORTANT
If you are doing SQL request in any language, make sure you are binding your parameters
Thanks to #Nicolas help, I was able to fix my problem. The final query is:
SELECT * FROM facturas JOIN clientes ON facturas.id_cliente=clientes.id_cliente where facturas.id_vendedor=$current_user (variable that I used to keep track of who is logged in)

Creating temp URLs in single page applications

In my react based single page application, my page is divided in two panes.
Left Pane: Filter Panel.
Right Pane: Grid (table containing data that passes through applied filters)
In summary, I have an application that looks very similar to amazon.com. By default, when user hits an application's root endpoint (/) in the browser, I fetch last 7 days of data from the server and show it inside the grid.
Filter panel has couple of filters (e.g. time filter to fetch data that falls inside specified time interval, Ids to search data with specific id etc.) and a search button attached in the header of filter panel. Hitting search button makes a post call to a server by giving selected filters inside post form body, server returns back data that matches filters passed and my frontend application displays this data returned back from the server inside grid.
Now, when someone hits search button in the filter panel I want to reflect selected filters in the query parameter of the URL, because it will help me to share these URLs with other users of my website, so that they can see filters I applied and see data inside the grid matching these filters only.
Problem here is, if on search button click, I use http get with query parameters, I will endup breaking application because of limit imposed on URL length by different browsers.
Please suggest me correct solution to create such URLs that will help me to set the selected filters in the filter panel without causing any side effect in my application.
Possible solution: Considering the fact that we cannot directly add plain strings in query parameter because of URL length limitation from different browsers (Note: Specification does not limit the length of an HTTP Get request but different browsers implement their own limitations), we can use something like message digest or hash (convert input of arbitrary length into an output of fixed length) and save it in DB for server to understand the request and serve content back. This is just a thought, I am not sure whether this is an ideal solution to this problem.
Behavior of other heavily used websites:
amazon.com, newegg.com -> uses hashed urls.
kayak.com -> since they have very well defined keywords, they use
short forms like IN for INDIA, BLR for Bangalore etc. and combine
this with negation logic to further optimize maximum url length. Not
checked but this will ideally break after large selection of filters.
flipkart.com -> appends strings directly to query parameters and breaks
after limit is breached. verified this.
In response to #cauchy's answer, we need to make a distinction between hashing and encryption.
Hashing
Hashes are by necessity irreversible. In order to map the hash to the specific filter combination, you would either need to
hash each permutation of filters on the server for every request to try matching the requested hash (computationally intensive) or
store a map of hash to filter combination on the server (memory intensive).
For the vast majority of cases, option 1 is going to be too slow. Depending on the number of filters and options, option B may require a sizable map, but it's still your best option.
Encryption
In this scheme, the server would send its public key to the client, then the client could use that to encrypt its filter options. The server would then decrypt the encrypted data with its private key. This is good, but your encrypted data will not be fixed length. So, as more options are selected, you run into the same problem of indeterminate parameter length.
Thus, in order to ensure your URL is short for any number of filters and options, you will need to maintain a mapping of hash->selection on the server.
How should we handle permanent vs temporary links?
You mentioned in your comment above
If we use some persistent store to save the mapping between this hash to actual filters, we would ideally want to segregate long-lived "permalinks" from short-lived ephemeral URLs, and use that understanding to efficiently expire the short-lived hashes.
You likely have a service on the server that handles all of the filters that you support in your application. The trick here is letting that service also manage the hashmap. As more filters and options are added/removed, the service will need to re-hash each permutation of filter selections.
If you need strong support for permalinks, then whenever you remove filters or options, you'll want to maintain the "expired" hashes and change their mapping to point to a reasonable alternative hash.
When do we update hashes in our DB?
There are lots of options, but I would generally prefer build time. If you're using a CI solution like Jenkins, Travis, AWS CodePipeline, etc., then you can add a build step to update your DB. Basically, you're going to...
Keep a persistent record of all the existing supported filters.
On build, check to see if there are any new filters. If so...
Add those filters to the record from step 1.
Hash all new filter permutations (just those that include your new filters) and store those in the hash DB
Check to see if any filters have been removed. If so...
Remove those filters from the record from step 1.
Find all the hashes for permutations that include those filters and either...
remove those hashes from the DB (weak permalinks), or
Point that hash to a reasonable alternative hash in the DB (strong permalinks)
Lets analyse your problem and the solution possible.
Problem : You want a URL which has information about the filter applied so that when you share that URL user doesn't land on arbitrary page.
Solutions:
1) Append filter applied with URL. To achieve this you will need to shorten the key of type of filter and the value of filter so that Length of URL don't exceed much for each filter.
Drawback: This is not most reliable solution as the number of filter increase URL length has to increase no other option.
2) Append a unique key of filter applied(hash) with URL. To achieve this you will need to do some changes on server and client both. On client side you will need a encoding algorithm which convert filter applied to unique hash. On server side you will need decoding algorithm which convert unique hash to filter applied. SO now client whenever a URL like this is hit you can make a POST api call which take this hash give you the array of filter applied or on client side only put the logic to convert this hash.
Do all this in componentWillMount to avoid any side effect.
I think 2nd solution is scalable and efficient in almost all cases.

Using AngularJs UI Router and global variables in single page application

I am creating a hybrid quiz application with AngularJS. I have below pseudo code,
index.html where user selects the quiz.
2nd page using ui-view Quiz is rendered and user selected options are stored in a global array (Should I use global here ?)
The values in array are compared with values in answer array (should answers array be global? This array is created by querying the database and on each new quiz this array has to change as this will select questions randomly.)
Will both the arrays be carried forward to the score view (when using them as local arrays or I need to to make them global or they will not be carried to next view?)
I am new to single page application and UI-Router so don't know how does it all work.
I don't think you should store user's selected options in Global variable because these aren't common variable to be used throughout the user session. You can save it in database when user redirects to 2nd page and retrieve on score view screen. Also, You may need this information in future to retain the user's records.
EDIT -
In case you can not store data in database and storing data at client side is only option then local storage can be used.

Categories

Resources