File size: 2,596 Bytes
8a6248c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
 * Transform Request Model
 * Represents requests from processors to buy raw materials and produce by-products
 */
import mongoose from "mongoose";

const transformRequestSchema = new mongoose.Schema(
  {
    processorId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Processor",
      required: true,
      index: true,
    },
    listingId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Listing",
    },
    sellerId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
    },
    requestType: {
      type: String,
      enum: ["spot_purchase", "contract_farming", "toll_processing", "buyback"],
      required: true,
    },
    rawMaterial: {
      productType: {
        type: String,
        required: true,
      },
      quantityKg: {
        type: Number,
        required: true,
        min: 0,
      },
      grade: String,
      pricePerKg: Number,
    },
    expectedOutput: [
      {
        productType: String,
        expectedQuantityKg: Number,
        expectedGrade: String,
      },
    ],
    processingFee: {
      amount: Number,
      currency: {
        type: String,
        default: "INR",
      },
      per: {
        type: String,
        enum: ["kg", "quintal", "ton", "batch"],
        default: "kg",
      },
    },
    timeline: {
      requestedDeliveryDate: Date,
      estimatedProcessingDays: Number,
      expectedCompletionDate: Date,
    },
    terms: {
      paymentMode: {
        type: String,
        enum: ["advance", "on_delivery", "credit", "escrow"],
        default: "escrow",
      },
      advancePercentage: Number,
      qualityStandards: String,
      penaltyClause: String,
    },
    status: {
      type: String,
      enum: [
        "pending",
        "accepted",
        "rejected",
        "in_progress",
        "completed",
        "cancelled",
        "disputed",
      ],
      default: "pending",
      index: true,
    },
    messages: [
      {
        senderId: mongoose.Schema.Types.ObjectId,
        message: String,
        createdAt: {
          type: Date,
          default: Date.now,
        },
      },
    ],
    completionDetails: {
      actualOutputQuantity: Number,
      qualityReport: String,
      deliveryDate: Date,
      invoiceNumber: String,
      invoiceUrl: String,
    },
  },
  {
    timestamps: true,
  }
);

// Indexes
transformRequestSchema.index({ processorId: 1, status: 1 });
transformRequestSchema.index({ sellerId: 1, status: 1 });

const TransformRequest = mongoose.model("TransformRequest", transformRequestSchema);

export default TransformRequest;