@classytic/payroll 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Validation Utilities - Fluent, Composable, Type-Safe
3
+ * Beautiful validation with clear semantics
4
+ */
5
+
6
+ export const isActive = (employee) =>
7
+ employee?.status === 'active';
8
+
9
+ export const isOnLeave = (employee) =>
10
+ employee?.status === 'on_leave';
11
+
12
+ export const isSuspended = (employee) =>
13
+ employee?.status === 'suspended';
14
+
15
+ export const isTerminated = (employee) =>
16
+ employee?.status === 'terminated';
17
+
18
+ export const isEmployed = (employee) =>
19
+ isActive(employee) || isOnLeave(employee) || isSuspended(employee);
20
+
21
+ export const canReceiveSalary = (employee) =>
22
+ isActive(employee) || isOnLeave(employee);
23
+
24
+ export const canUpdateEmployment = (employee) =>
25
+ !isTerminated(employee);
26
+
27
+ export const isInProbation = (employee, now = new Date()) =>
28
+ employee?.probationEndDate && new Date(employee.probationEndDate) > now;
29
+
30
+ export const hasCompletedProbation = (employee, now = new Date()) =>
31
+ employee?.probationEndDate && new Date(employee.probationEndDate) <= now;
32
+
33
+ export const hasCompensation = (employee) =>
34
+ employee?.compensation?.baseAmount > 0;
35
+
36
+ export const isEligibleForBonus = (employee, requiredMonths = 6) => {
37
+ if (!isActive(employee)) return false;
38
+ const monthsEmployed = monthsBetween(employee.hireDate, new Date());
39
+ return monthsEmployed >= requiredMonths;
40
+ };
41
+
42
+ export const isValidCompensation = (compensation) =>
43
+ compensation?.baseAmount > 0 &&
44
+ compensation?.frequency &&
45
+ compensation?.currency;
46
+
47
+ export const isValidBankDetails = (bankDetails) =>
48
+ bankDetails?.accountNumber &&
49
+ bankDetails?.bankName &&
50
+ bankDetails?.accountHolderName;
51
+
52
+ export const hasRequiredFields = (obj, fields) =>
53
+ fields.every((field) => obj?.[field] !== undefined && obj?.[field] !== null);
54
+
55
+ export const createValidator = (validationFns) => (data) => {
56
+ const errors = [];
57
+
58
+ for (const [field, validator] of Object.entries(validationFns)) {
59
+ const result = validator(data[field], data);
60
+ if (result !== true) {
61
+ errors.push({ field, message: result });
62
+ }
63
+ }
64
+
65
+ return {
66
+ isValid: errors.length === 0,
67
+ errors,
68
+ };
69
+ };
70
+
71
+ export const required = (fieldName) => (value) =>
72
+ value !== undefined && value !== null && value !== ''
73
+ ? true
74
+ : `${fieldName} is required`;
75
+
76
+ export const min = (minValue, fieldName) => (value) =>
77
+ value >= minValue ? true : `${fieldName} must be at least ${minValue}`;
78
+
79
+ export const max = (maxValue, fieldName) => (value) =>
80
+ value <= maxValue ? true : `${fieldName} must not exceed ${maxValue}`;
81
+
82
+ export const inRange = (minValue, maxValue, fieldName) => (value) =>
83
+ value >= minValue && value <= maxValue
84
+ ? true
85
+ : `${fieldName} must be between ${minValue} and ${maxValue}`;
86
+
87
+ export const oneOf = (allowedValues, fieldName) => (value) =>
88
+ allowedValues.includes(value)
89
+ ? true
90
+ : `${fieldName} must be one of: ${allowedValues.join(', ')}`;
91
+
92
+ export const compose = (...validators) => (value, data) => {
93
+ for (const validator of validators) {
94
+ const result = validator(value, data);
95
+ if (result !== true) return result;
96
+ }
97
+ return true;
98
+ };
99
+
100
+ // Additional validators
101
+ export const isPositive = (fieldName) => (value) =>
102
+ value > 0 ? true : `${fieldName} must be positive`;
103
+
104
+ export const isValidStatus = (value) =>
105
+ ['active', 'on_leave', 'suspended', 'terminated'].includes(value);
106
+
107
+ export const isValidEmploymentType = (value) =>
108
+ ['full_time', 'part_time', 'contract', 'internship'].includes(value);
109
+
110
+ // Aliases for consistency
111
+ export const minValue = min;
112
+ export const maxValue = max;
113
+ export const isInRange = inRange;
114
+
115
+ const monthsBetween = (start, end) => {
116
+ const startDate = new Date(start);
117
+ const endDate = new Date(end);
118
+ return (
119
+ (endDate.getFullYear() - startDate.getFullYear()) * 12 +
120
+ (endDate.getMonth() - startDate.getMonth())
121
+ );
122
+ };