How to allow 1-1 or 1-to many communication depending on the type of user?

So in the website that I’m working on(this is a real estate website), I have two types of users: agents and clients. When client makes a request to connect with whatever agent they want to connect with, I want the communication for the client to be 1-1. Meaning for each client, they can only talk with one agent at a time. In contrast to this, agents should be 1-to many. In other words, I want for every agent to be able to talk to multiple clients at a time(here the agent would be communicating with multiple clients at a time; each client would be unique of course).

I assume I would have to create two models in my PostgreSQL database: agent and client and each would have some type of association with each other and send messages based on their id. Still, I’m not sure what would be a good way to manage this. Any help you guys provide would be much appreciated thanks.

I think that would be the right way to start off for the persistence part.

I am seeing you using the time to be the key here so I would have a relation that has fields - time-frame, client, and agent. I would set the unique index on fields - time-frame, and client here so that at a particular time frame and single client and communicate with a single agent. This also capture an agent can communicate with many clients.

note: time-frame here might be compose of fields start-time and end-time for might be another fields that suit the application needs.

Are you interested in historical data though? One month ago user X might have communicated exclusively with agent Y but what about after the communication is completed? If you are not interested in history then I’d guess agent_id inside the User record would be quite enough.

2 Likes

That might be something interesting to implement down the road but for now I’m simply trying to establish a connection between an agent and a client via those associations.

Then you already have your answer. user.agent_id = agent.id is quite enough. Once the communication is finished, just do user.agent_id = nil to make it known that the user is now not communicating with any agent. Rinse and repeat.

1 Like