# All files / middleware/validators invoiceValidation.js

0% Statements 0/14
100% Branches 0/0
0% Functions 0/1
0% Lines 0/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334

     /**
     * Invoice Routes Validation
     * Validation rules for invoice and quote endpoints
     *
     * @module middleware/validators/invoiceValidation
     */

    const { body, param, query } = require('express-validator');
    const { handleValidationErrors } = require('../validation');

    /**
     * Validate invoice ID parameter
     */
    const validateInvoiceId = [
      param('id')
        .isInt({ min: 1 })
        .withMessage('Invalid invoice ID'),
      handleValidationErrors
    ];

    /**
     * Validate invoice number parameter
     */
    const validateInvoiceNumber = [
      param('invoice_number')
        .trim()
        .notEmpty()
        .withMessage('Invoice number is required')
        .matches(/^[A-Z]{3}-\d{4}-\d{5}$/)
        .withMessage('Invalid invoice number format'),
      handleValidationErrors
    ];

    /**
     * Validate create invoice request
     */
    const validateCreateInvoice = [
      body('type')
        .isIn(['invoice', 'quote'])
        .withMessage('Type must be invoice or quote'),
      body('title')
        .trim()
        .notEmpty()
        .withMessage('Title is required')
        .isLength({ min: 3, max: 200 })
        .withMessage('Title must be 3-200 characters'),
      body('description')
        .optional({ nullable: true })
        .trim()
        .isLength({ max: 2000 })
        .withMessage('Description too long (max 2000 characters)'),
      body('currency')
        .optional()
        .isIn(['USD', 'BRL', 'EUR', 'GBP'])
        .withMessage('Invalid currency'),
      body('tax_rate')
        .optional()
        .isFloat({ min: 0, max: 100 })
        .withMessage('Tax rate must be between 0 and 100'),
      body('discount_type')
        .optional()
        .isIn(['fixed', 'percentage'])
        .withMessage('Discount type must be fixed or percentage'),
      body('discount_value')
        .optional()
        .isFloat({ min: 0 })
        .withMessage('Discount value must be positive'),
      body('payment_method')
        .optional()
        .isIn(['paypal', 'pagseguro', 'stripe', 'pix', 'bank_transfer', 'cash', 'other'])
        .withMessage('Invalid payment method'),
      body('due_date')
        .optional({ nullable: true })
        .isISO8601()
        .withMessage('Invalid due date format'),
      body('notes')
        .optional({ nullable: true })
        .trim()
        .isLength({ max: 2000 })
        .withMessage('Notes too long (max 2000 characters)'),
      body('terms')
        .optional({ nullable: true })
        .trim()
        .isLength({ max: 2000 })
        .withMessage('Terms too long (max 2000 characters)'),

      // Client validation
      body('client')
        .isObject()
        .withMessage('Client information is required'),
      body('client.name')
        .trim()
        .notEmpty()
        .withMessage('Client name is required')
        .isLength({ min: 2, max: 100 })
        .withMessage('Client name must be 2-100 characters'),
      body('client.email')
        .trim()
        .notEmpty()
        .withMessage('Client email is required')
        .isEmail()
        .withMessage('Invalid email format')
        .normalizeEmail(),
      body('client.phone')
        .trim()
        .notEmpty()
        .withMessage('Client phone is required')
        .customSanitizer(value => value.replace(/[\s\-\(\)\.]/g, ''))
        .matches(/^\+?[0-9]{8,15}$/)
        .withMessage('Invalid phone number format (8-15 digits)'),
      body('client.company_name')
        .optional({ nullable: true })
        .trim()
        .isLength({ max: 150 })
        .withMessage('Company name too long'),
      body('client.tax_id')
        .optional({ nullable: true })
        .trim()
        .isLength({ max: 50 })
        .withMessage('Tax ID too long'),
      body('client.address')
        .optional({ nullable: true })
        .trim()
        .isLength({ max: 500 })
        .withMessage('Address too long'),
      body('client.city')
        .optional({ nullable: true })
        .trim()
        .isLength({ max: 100 })
        .withMessage('City too long'),
      body('client.state')
        .optional({ nullable: true })
        .trim()
        .isLength({ max: 50 })
        .withMessage('State too long'),
      body('client.zip_code')
        .optional({ nullable: true })
        .trim()
        .isLength({ max: 20 })
        .withMessage('Zip code too long'),
      body('client.country')
        .optional({ nullable: true })
        .trim()
        .isLength({ max: 50 })
        .withMessage('Country too long'),

      // Items validation
      body('items')
        .isArray({ min: 1 })
        .withMessage('At least one item is required'),
      body('items.*.description')
        .trim()
        .notEmpty()
        .withMessage('Item description is required')
        .isLength({ min: 2, max: 255 })
        .withMessage('Item description must be 2-255 characters'),
      body('items.*.quantity')
        .isFloat({ min: 0.01 })
        .withMessage('Item quantity must be positive'),
      body('items.*.unit_price')
        .isFloat({ min: 0 })
        .withMessage('Item unit price must be positive or zero'),

      handleValidationErrors
    ];

    /**
     * Validate update status request
     */
    const validateUpdateStatus = [
      param('id')
        .isInt({ min: 1 })
        .withMessage('Invalid invoice ID'),
      body('status')
        .isIn(['draft', 'sent', 'viewed', 'accepted', 'rejected', 'paid', 'cancelled', 'archived'])
        .withMessage('Invalid status'),
      handleValidationErrors
    ];

    /**
     * Validate send invoice request
     */
    const validateSendInvoice = [
      param('id')
        .isInt({ min: 1 })
        .withMessage('Invalid invoice ID'),
      body('phone')
        .optional({ nullable: true })
        .trim()
        .matches(/^\+?[0-9]{8,15}$/)
        .withMessage('Invalid phone number format'),
      body('use_client_phone')
        .optional()
        .isBoolean()
        .withMessage('use_client_phone must be boolean'),
      handleValidationErrors
    ];

    /**
     * Validate invoice list filters
     */
    const validateInvoiceFilters = [
      query('type')
        .optional()
        .isIn(['invoice', 'quote'])
        .withMessage('Type must be invoice or quote'),
      query('status')
        .optional()
        .isIn(['draft', 'sent', 'viewed', 'accepted', 'rejected', 'paid', 'cancelled', 'archived'])
        .withMessage('Invalid status'),
      query('tab')
        .optional()
        .isIn(['active', 'archived', 'disabled'])
        .withMessage('Tab must be active, archived, or disabled'),
      query('client_id')
        .optional()
        .isInt({ min: 1 })
        .withMessage('Invalid client ID'),
      query('search')
        .optional()
        .trim()
        .isLength({ min: 1, max: 100 })
        .withMessage('Search query must be 1-100 characters'),
      query('page')
        .optional()
        .isInt({ min: 1 })
        .withMessage('Invalid page number'),
      query('limit')
        .optional()
        .isInt({ min: 1, max: 1000 })
        .withMessage('Invalid limit (1-1000)'),
      query('sort_by')
        .optional()
        .isIn(['created_at', 'updated_at', 'due_date', 'total_amount', 'invoice_number'])
        .withMessage('Invalid sort field'),
      query('sort_order')
        .optional()
        .isIn(['ASC', 'DESC'])
        .withMessage('Sort order must be ASC or DESC'),
      handleValidationErrors
    ];

    /**
     * Validate public accept request
     */
    const validatePublicAccept = [
      param('invoice_number')
        .trim()
        .notEmpty()
        .withMessage('Invoice number is required'),
      body('email')
        .trim()
        .notEmpty()
        .withMessage('Email is required')
        .isEmail()
        .withMessage('Invalid email format')
        .normalizeEmail(),
      body('payment_method')
        .optional()
        .trim()
        .isIn(['paypal', 'stripe'])
        .withMessage('Invalid payment method. Use: paypal, stripe'),
      handleValidationErrors
    ];

    /**
     * Validate public reject request
     */
    const validatePublicReject = [
      param('invoice_number')
        .trim()
        .notEmpty()
        .withMessage('Invoice number is required'),
      body('email')
        .trim()
        .notEmpty()
        .withMessage('Email is required')
        .isEmail()
        .withMessage('Invalid email format')
        .normalizeEmail(),
      body('reason')
        .trim()
        .notEmpty()
        .withMessage('Rejection reason is required')
        .isLength({ min: 7, max: 1000 })
        .withMessage('Rejection reason must be 7-1000 characters'),
      handleValidationErrors
    ];

    /**
     * Validate public mark paid request
     */
    const validatePublicMarkPaid = [
      param('invoice_number')
        .trim()
        .notEmpty()
        .withMessage('Invoice number is required'),
      body('email')
        .trim()
        .notEmpty()
        .withMessage('Email is required')
        .isEmail()
        .withMessage('Invalid email format')
        .normalizeEmail(),
      handleValidationErrors
    ];

    /**
     * Validate client email query
     */
    const validateClientEmail = [
      query('email')
        .trim()
        .notEmpty()
        .withMessage('Email is required')
        .isEmail()
        .withMessage('Invalid email format')
        .normalizeEmail(),
      handleValidationErrors
    ];

    module.exports = {
      validateInvoiceId,
      validateInvoiceNumber,
      validateCreateInvoice,
      validateUpdateStatus,
      validateSendInvoice,
      validateInvoiceFilters,
      validatePublicAccept,
      validatePublicReject,
      validatePublicMarkPaid,
      validateClientEmail
    };