Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chat in AIS to Exhibitor #1241

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useConfirmSaveAlert } from "@/shared/ConfirmSaveAlert"
import { useDashboard } from "@/shared/hooks/api/useDashboard"
import { HOST } from "@/shared/vars"
import { cn } from "@/utils/cx"
import { ErrorMessage } from "@hookform/error-message"
//import { ErrorMessage } from "@hookform/error-message"
import { zodResolver } from "@hookform/resolvers/zod"
import { useMutation, useQueryClient } from "@tanstack/react-query"
import { useBlocker, useParams } from "@tanstack/react-router"
Expand Down
8 changes: 5 additions & 3 deletions apps/dashboard/src/screens/dashboard/ContactBubble.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { Separator } from "@/components/ui/separator"
import { useDashboard } from "@/shared/hooks/api/useDashboard"
import { Mail, MessageCircleQuestion, Phone, X } from "lucide-react"
//giimport {Chat} from "@/screens/dashboard/ExibitorChat"

export function ContactBubble() {
const { data } = useDashboard()
Expand All @@ -19,11 +20,10 @@ export function ContactBubble() {
const additionalCompanyContacts = data?.sales_contacts?.slice(1)

if (companyContact == null) return null

return (
<Drawer>
<DrawerTrigger>
<div className="fixed bottom-5 right-10 h-20 w-20 rounded-full transition-all duration-200 active:scale-95">
<div className="fixed bottom-10 right-10 h-20 w-20 rounded-full transition-all duration-200 active:scale-95">
<div className="absolute -right-2 -top-2 flex h-10 w-10 items-center justify-center rounded-full bg-emerald-100">
<MessageCircleQuestion
size={20}
Expand Down Expand Up @@ -68,6 +68,8 @@ export function ContactBubble() {
{companyContact.phone_number}
</a>
</p>
<p className="text-sm text-stone-400">
</p>
</div>
</DrawerDescription>
</div>
Expand All @@ -89,7 +91,7 @@ export function ContactBubble() {
<DrawerTitle>Secondary Contacts</DrawerTitle>
<div className="flex flex-wrap justify-between gap-4">
{additionalCompanyContacts.map(contact => (
<div>
<div key={contact.email}>
<p className="text-sm">
{contact.first_name} -{" "}
{contact.title}
Expand Down
5 changes: 5 additions & 0 deletions apps/dashboard/src/screens/dashboard/ExibitorChat.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

export function Chat() {

return
}
34 changes: 34 additions & 0 deletions apps/dashboard/src/screens/dashboard/aismessages/aismessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import axios from 'axios';

// Base API client configuration (adjust the baseURL)
const apiClient = axios.create({
baseURL: 'http://your-python-api-url.com/api', // replace with your actual API URL
headers: {
'Content-Type': 'application/json',
},
});

// Fetch all messages
export const fetchMessages = async (contactId: string) => {
try {
const response = await apiClient.get(`/messages?contactId=${contactId}`);
return response.data;
} catch (error) {
console.error('Error fetching messages:', error);
throw error;
}
};

// Send a new message
export const sendMessage = async (contactId: string, message: string) => {
try {
const response = await apiClient.post('/messages', {
contactId,
message,
});
return response.data;
} catch (error) {
console.error('Error sending message:', error);
throw error;
}
};
Empty file.
1 change: 0 additions & 1 deletion companies/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from .models import (
Company,
CompanyAddress,
CompanyCustomer,
CompanyCustomerResponsible,
CompanyContact,
CompanyCustomerComment,
Expand Down
51 changes: 51 additions & 0 deletions companies/migrations/0033_companycustomerchat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Generated by Django 2.2.24 on 2024-06-11 13:04

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("companies", "0032_add_company_type_default"),
]

operations = [
migrations.CreateModel(
name="CompanyCustomerChat",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("comment", models.TextField()),
("timestamp", models.DateTimeField(auto_now_add=True)),
("show_in_exhibitors", models.BooleanField(default=False)),
(
"company",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="companies.Company",
),
),
("groups", models.ManyToManyField(blank=True, to="companies.Group")),
(
"user",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"ordering": ["-timestamp"],
},
),
]
26 changes: 26 additions & 0 deletions companies/migrations/0034_update_chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 2.2.24 on 2024-06-14 14:56

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('companies', '0033_companycustomerchat'),
]

operations = [
migrations.RenameField(
model_name='companycustomerchat',
old_name='comment',
new_name='chat',
),
migrations.RemoveField(
model_name='companycustomerchat',
name='groups',
),
migrations.RemoveField(
model_name='companycustomerchat',
name='show_in_exhibitors',
),
]
59 changes: 58 additions & 1 deletion exhibitors/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@
from django.views.decorators.http import require_POST
from django.views.decorators.csrf import csrf_exempt

from companies.models import CompanyContact
from exhibitors import serializers
from exhibitors.models import Exhibitor, Location, Booth, ExhibitorInBooth, LocationTick
from exhibitors.models import (
Exhibitor,
Location,
Booth,
ExhibitorInBooth,
LocationTick,
ExhibitorChat,
)
from fair.models import Fair, FairDay
from util.permission import UserPermission

MISSING_IMAGE = "/static/missing.png"

Expand Down Expand Up @@ -305,6 +314,54 @@ def create_booth(request, location_pk):
return JsonResponse({"booth": serializers.booth(booth)}, status=201)


@require_POST
def post_chat_message(request, exhibitor_pk):

if not request.user:
return JsonResponse({"message": "Authentication required."}, status=403)

exhibitor = get_object_or_404(Exhibitor, pk=exhibitor_pk)

company = exhibitor.company
contact = CompanyContact.objects.get(user=request.user, company=company)

if not contact:
return JsonResponse({"message": "Authentication required."}, status=403)

data = json.load(request.body)

chat = ExhibitorChat.objects.create(
exhibitor=exhibitor,
user=request.user,
is_armada_response=False,
chat=data["chat"],
)
return JsonResponse({"chat": serializers.exhibitor_chat(chat)}, status=201)


def chat_messages(request, exhibitor_pk):
if not request.user:
return JsonResponse({"message": "Authentication required."}, status=403)

exhibitor = get_object_or_404(Exhibitor, pk=exhibitor_pk)

permission = UserPermission(request.user)
user_is_contact_person = request.user in exhibitor.contact_persons.all()

allowed = (
permission == UserPermission.SALES
or user_is_contact_person
or CompanyContact.objects.get(user=request.user, company=exhibitor.company)
)
if not allowed:
return JsonResponse({"message": "Authentication required."}, status=403)

chats = ExhibitorChat.objects.filter(exhibitor=exhibitor)
chat_list = [serializers.exhibitor_chat(chat) for chat in chats]

return JsonResponse({"chats": chat_list})


@require_POST
@csrf_exempt
def people_count(request, location_pk):
Expand Down
2 changes: 2 additions & 0 deletions exhibitors/api_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ def cors_allow_api_to_exhibitors(sender, request, **kwargs):
url(r"^locations/(?P<location_pk>[0-9]+)/create_booth$", api.create_booth),
url(r"^locations/(?P<location_pk>[0-9]+)/people_count$", api.people_count),
url(r"^days$", api.days),
url(r"^chats/(?P<exhibitor_pk>[0-9]+)$", api.chat_messages),
url(r"^chats/(?P<exhibitor_pk>[0-9]+)/send_message$", api.post_chat_message),
]
11 changes: 10 additions & 1 deletion exhibitors/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import inspect

from companies.models import Company, CompanyCustomerComment
from .models import ExhibitorView, Exhibitor, Booth, ExhibitorInBooth
from .models import ExhibitorChat, ExhibitorView, Exhibitor, Booth, ExhibitorInBooth

from dal import autocomplete

Expand Down Expand Up @@ -126,6 +126,15 @@ class Meta:
fields = ["comment"]


class ChatForm(forms.ModelForm):
class Meta:
model = ExhibitorChat
fields = ["chat"]
widgets = {
"chat": forms.Textarea(attrs={"class": "form-control", "rows": 3}),
}


class BoothForm(forms.ModelForm):
class Meta:
model = Booth
Expand Down
29 changes: 29 additions & 0 deletions exhibitors/migrations/0072_addExhibitorChat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 2.2.24 on 2024-07-28 14:47

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('exhibitors', '0071_add_exhibitor_tier'),
]

operations = [
migrations.CreateModel(
name='ExhibitorChat',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('chat', models.TextField()),
('timestamp', models.DateTimeField(auto_now_add=True)),
('exhibitor', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='exhibitors.Exhibitor')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
options={
'ordering': ['-timestamp'],
},
),
]
31 changes: 31 additions & 0 deletions exhibitors/migrations/0073_addExhibitorChat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 2.2.24 on 2024-07-28 18:07

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('exhibitors', '0072_addExhibitorChat'),
]

operations = [
migrations.AddField(
model_name='exhibitorchat',
name='is_armada_response',
field=models.BooleanField(default=True),
preserve_default=False,
),
migrations.AlterField(
model_name='exhibitorchat',
name='exhibitor',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='exhibitors.Exhibitor'),
),
migrations.AlterField(
model_name='exhibitorchat',
name='user',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL),
),
]
14 changes: 14 additions & 0 deletions exhibitors/migrations/0076_merge_20240925_1340.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Django 2.2.24 on 2024-09-25 13:40

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('exhibitors', '0073_addExhibitorChat'),
('exhibitors', '0075_change_exhibitor_application_status'),
]

operations = [
]
18 changes: 18 additions & 0 deletions exhibitors/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,24 @@ class Meta:
]


# After signing the contract it should be possible to write comments to the CRM
class ExhibitorChat(models.Model):
exhibitor = models.ForeignKey(
Exhibitor, null=True, on_delete=models.SET_NULL, db_index=True
)
user = models.ForeignKey(User, null=True, on_delete=models.SET_NULL)
is_armada_response = models.BooleanField()

chat = models.TextField(null=False, blank=False)
timestamp = models.DateTimeField(null=False, blank=False, auto_now_add=True)

class Meta:
ordering = ["-timestamp"]

def __str__(self):
return self.chat


class FairLocationSpecial(models.Model):
name = models.CharField(blank=True, null=True, max_length=255)
fair = models.ForeignKey("fair.Fair", on_delete=models.CASCADE)
Expand Down
Loading