File size: 2,066 Bytes
ae677bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from rest_framework import serializers
from .models import PropertyCustomQA

class PropertyCustomQASerializer(serializers.ModelSerializer):
    class Meta:
        model = PropertyCustomQA
        fields = ['id', 'question', 'answer', 'order', 'is_active']
        read_only_fields = ['id', 'created_at', 'updated_at']
    
    def validate_question(self, value):
        if len(value.strip()) < 5:
            raise serializers.ValidationError("Question must be at least 5 characters")
        return value.strip()
    
    def validate_answer(self, value):
        if len(value.strip()) < 5:
            raise serializers.ValidationError("Answer must be at least 5 characters")
        return value.strip()


class EnableAutoChatSerializer(serializers.Serializer):
    """Serializer for enabling auto-chat with custom questions"""
    property_id = serializers.UUIDField()
    enable = serializers.BooleanField(default=True)
    custom_questions = serializers.ListField(
        child=PropertyCustomQASerializer(),
        required=False,
        max_length=5,  # Max 5 questions
        min_length=0
    )
    
    def validate_custom_questions(self, value):
        if len(value) > 5:
            raise serializers.ValidationError("Maximum 5 custom questions allowed")
        return value
    
    def validate(self, data):
        # Check if all master questions are answered first
        from .models import MasterQuestion, PropertyQA
        
        property_id = data['property_id']
        total_master = MasterQuestion.objects.filter(is_active=True).count()
        answered_master = PropertyQA.objects.filter(
            property_id=property_id,
            master_question__is_active=True
        ).count()
        
        if answered_master < total_master:
            raise serializers.ValidationError(
                f"Please answer all {total_master} master questions first. "
                f"Currently answered: {answered_master}/{total_master}"
            )
        
        return data