-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvendor_example.js
More file actions
299 lines (272 loc) · 10.6 KB
/
Copy pathvendor_example.js
File metadata and controls
299 lines (272 loc) · 10.6 KB
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
/**
* OpenMAPS/CL — Ejemplo de implementación en JavaScript / Node.js
* ================================================================
* Demuestra cómo un software de agenda médica publica disponibilidad
* de horas mediante el estándar OpenMAPS/CL.
*
* Autor: Bernardo Cajales Millón
* Licencia: MIT
* Estándar: https://github.com/bcajales/openmaps-standard
*/
"use strict";
// ── Códigos de especialidad SIS-MINSAL ────────────────────────────────────────
const SIS_MINSAL = {
"01": "ANATOMÍA PATOLÓGICA",
"02": "ANESTESIOLOGÍA",
"03": "CARDIOLOGÍA",
"04": "CARDIOCIRUGÍA",
"05": "CIRUGÍA GENERAL",
"06": "CIRUGÍA PEDIÁTRICA",
"07": "CIRUGÍA PLÁSTICA Y REPARADORA",
"08": "CIRUGÍA TORÁCICA",
"09": "CIRUGÍA VASCULAR PERIFÉRICA",
"10": "DERMATOLOGÍA",
"11": "ENDOCRINOLOGÍA",
"12": "ENFERMEDADES RESPIRATORIAS",
"13": "GASTROENTEROLOGÍA",
"14": "GENÉTICA CLÍNICA",
"15": "GERIATRÍA",
"16": "GINECOLOGÍA",
"17": "HEMATOLOGÍA",
"18": "INFECTOLOGÍA",
"19": "INMUNOLOGÍA",
"20": "MEDICINA FÍSICA Y REHABILITACIÓN",
"21": "MEDICINA INTERNA",
"22": "MEDICINA NUCLEAR",
"23": "NEFROLOGÍA",
"24": "NEONATOLOGÍA",
"25": "NEUROCIRUGÍA",
"26": "NEUROLOGÍA",
"27": "NUTRIOLOGÍA",
"28": "OBSTETRICIA",
"29": "OFTALMOLOGÍA",
"30": "ONCOLOGÍA MÉDICA",
"31": "ORTOPEDIA Y TRAUMATOLOGÍA",
"32": "OTORRINOLARINGOLOGÍA",
"33": "PEDIATRÍA",
"34": "PSIQUIATRÍA",
"35": "RADIOLOGÍA",
"36": "REUMATOLOGÍA",
"37": "UROLOGÍA",
"38": "MEDICINA GENERAL/FAMILIAR",
"39": "ODONTOLOGÍA GENERAL",
"40": "PSICOLOGÍA",
};
// ── Constructor de prestador ───────────────────────────────────────────────────
/**
* Construye un objeto provider en formato OpenMAPS/CL.
* Mapea al recurso Schedule de FHIR R4.
*
* @param {Object} params
* @returns {Object} Objeto provider OpenMAPS/CL
*/
function construirPrestador({
providerId,
name,
providerType, // clinic | hospital | medical_center | independent_professional |
// dental | laboratory | imaging_center | rehabilitation
rutProvider, // Formato: XX.XXX.XXX-X
acceptsFonasa = false,
fonasaAccreditationLevel = null, // "1", "2" o "3"
libreEleccion = false,
superintendenciaCode = null,
address,
commune,
city,
region = null,
lat = null,
lng = null,
phone = null, // Formato E.164: +56XXXXXXXXX
email = null,
website = null,
}) {
const provider = {
provider_id: providerId,
name,
provider_type: providerType,
rut_provider: rutProvider,
accepts_fonasa: acceptsFonasa,
libre_eleccion: libreEleccion,
location: { address, commune, city, country: "CL" },
};
if (region) provider.location.region = region;
if (lat !== null && lng !== null) { provider.location.latitude = lat; provider.location.longitude = lng; }
if (fonasaAccreditationLevel) provider.fonasa_accreditation_level = fonasaAccreditationLevel;
if (superintendenciaCode) provider.superintendencia_code = superintendenciaCode;
if (phone || email) {
provider.contact = {};
if (phone) provider.contact.phone = phone;
if (email) provider.contact.email = email;
}
if (website) provider.website = website;
return provider;
}
// ── Utilidad de fechas ───────────────────────────────────────────────────────
/**
* Suma minutos a un string ISO 8601 preservando el offset de zona horaria original.
* Evita Date.toISOString() que siempre convierte a UTC (sufijo Z).
*
* @param {string} isoString — Fecha ISO 8601 con offset (ej. "2026-03-21T10:00:00-03:00")
* @param {number} minutes — Minutos a sumar
* @returns {string} Fecha ISO 8601 con el mismo offset
*/
function addMinutesToISO(isoString, minutes) {
const offsetMatch = isoString.match(/([+-]\d{2}:\d{2})$/);
const offset = offsetMatch ? offsetMatch[1] : "+00:00";
const date = new Date(isoString);
const result = new Date(date.getTime() + minutes * 60 * 1000);
const offsetSign = offset[0] === "+" ? 1 : -1;
const offsetH = parseInt(offset.slice(1, 3), 10);
const offsetM = parseInt(offset.slice(4, 6), 10);
const offsetMs = offsetSign * (offsetH * 60 + offsetM) * 60 * 1000;
const local = new Date(result.getTime() + offsetMs);
const pad = (n) => String(n).padStart(2, "0");
const y = local.getUTCFullYear();
const mo = pad(local.getUTCMonth() + 1);
const d = pad(local.getUTCDate());
const h = pad(local.getUTCHours());
const mi = pad(local.getUTCMinutes());
const s = pad(local.getUTCSeconds());
return `${y}-${mo}-${d}T${h}:${mi}:${s}${offset}`;
}
// ── Constructor de hora médica ────────────────────────────────────────────────
/**
* Construye un objeto slot en formato OpenMAPS/CL.
* Mapea al recurso Slot de FHIR R4.
*
* @param {Object} params
* @returns {Object} Objeto slot OpenMAPS/CL
*/
function construirHora({
slotId, // Identificador único y estable en el sistema del vendor
providerId,
sisCode, // Código SIS-MINSAL de la especialidad
startDatetime, // ISO 8601 con offset de zona horaria obligatorio
durationMinutes = 30,
professionalName = null,
rutProfessional = null,
modality = "in_person", // in_person | telemedicine | home_visit
acceptsFonasa = false,
fonasaAccreditationLevel = null,
insuranceAccepted = ["FONASA-B", "FONASA-C", "FONASA-D", "FONASA-LE", "ISAPRE", "PARTICULAR"],
libreEleccion = false,
isGes = false,
gesProblemCode = null,
basePrice = null, // Precio particular en CLP
}) {
const endDatetime = addMinutesToISO(startDatetime, durationMinutes);
const ges = { is_ges: isGes };
if (isGes && gesProblemCode) ges.ges_problem_code = gesProblemCode;
const slot = {
slot_id: slotId,
provider_id: providerId,
specialty: {
sis_code: sisCode,
sis_name: SIS_MINSAL[sisCode] || "",
},
start_datetime: startDatetime,
end_datetime: endDatetime,
duration_minutes: durationMinutes,
status: "available",
modality,
accepts_fonasa: acceptsFonasa,
insurance_accepted: insuranceAccepted,
libre_eleccion: libreEleccion,
ges,
currency: "CLP",
_schema: "openmaps-cl/0.1",
};
if (professionalName) slot.professional_name = professionalName;
if (rutProfessional) slot.rut_professional = rutProfessional;
if (fonasaAccreditationLevel) slot.fonasa_accreditation_level = fonasaAccreditationLevel;
if (basePrice !== null) slot.base_price = basePrice;
return slot;
}
// ── Constructor del payload ────────────────────────────────────────────────────
/**
* Construye el payload completo de disponibilidad en formato OpenMAPS/CL
* listo para enviar al endpoint POST /openmaps-cl/v1/slots.
*
* @param {string} vendorId
* @param {Object[]} prestadores
* @param {Object[]} horas
* @returns {Object} Payload OpenMAPS/CL completo
*/
function construirPayload(vendorId, prestadores, horas) {
return {
schema: "openmaps-cl/0.1",
vendor_id: vendorId,
generated_at: new Date().toISOString(),
providers: prestadores,
slots: horas,
};
}
// ── Exportar para uso como módulo ─────────────────────────────────────────────
module.exports = {
construirPrestador,
construirHora,
construirPayload,
addMinutesToISO,
SIS_MINSAL,
};
// ── Ejemplo de uso ────────────────────────────────────────────────────────────
if (require.main === module) {
const prestador = construirPrestador({
providerId: "MIVDR-PROV-001",
name: "Centro Médico Ejemplo",
providerType: "medical_center",
rutProvider: "76.543.210-1",
acceptsFonasa: true,
fonasaAccreditationLevel: "2", // Obtenido del Registro Nacional de Prestadores
libreEleccion: true,
address: "Av. Providencia 1234",
commune: "Providencia",
city: "Santiago",
region: "Región Metropolitana",
lat: -33.4294,
lng: -70.6148,
phone: "+56223456789",
email: "contacto@centromedico.cl",
website: "https://centromedico.cl",
});
// Generar horas disponibles para los próximos 3 días
// Se construyen los strings ISO 8601 directamente con offset de Chile (-03:00)
// para evitar que setHours() opere en la zona horaria local del servidor.
const horas = [];
const bloquesHorarios = [9, 10, 11, 15, 16];
const CHILE_OFFSET = "-03:00"; // Horario de verano de Chile (sep–abr). En invierno (abr–sep) usar "-04:00".
const fechaBase = new Date(Date.UTC(2026, 2, 21)); // 2026-03-21, solo para aritmética de fechas
for (let dia = 0; dia < 3; dia++) {
for (const hora of bloquesHorarios) {
const d = new Date(fechaBase);
d.setUTCDate(d.getUTCDate() + dia);
const year = d.getUTCFullYear();
const month = String(d.getUTCMonth() + 1).padStart(2, "0");
const day = String(d.getUTCDate()).padStart(2, "0");
const hh = String(hora).padStart(2, "0");
const startDatetime = `${year}-${month}-${day}T${hh}:00:00${CHILE_OFFSET}`;
const slotId = `MIVDR-PROV-001-DER-${year}${month}${day}${hh}00`;
horas.push(construirHora({
slotId,
providerId: "MIVDR-PROV-001",
sisCode: "10", // DERMATOLOGÍA
startDatetime,
durationMinutes: 30,
professionalName: "Dra. María González",
rutProfessional: "15.234.567-8",
acceptsFonasa: true,
fonasaAccreditationLevel: "2",
libreEleccion: true,
basePrice: 48000, // Precio particular — el consumidor calcula copagos
}));
}
}
const payload = construirPayload("mi-software-de-agenda", [prestador], horas);
console.log(JSON.stringify(payload, null, 2));
console.log(`\n✓ ${horas.length} horas para 1 prestador generadas`);
console.log(` Especialidad: ${SIS_MINSAL["10"]} (código 10)`);
console.log(` Precio particular (base_price): $${(48000).toLocaleString("es-CL")} CLP`);
console.log(` Nivel acreditación Fonasa LE: 2`);
console.log(` El sistema consumidor determina los copagos por tramo`);
console.log(` según el arancel público de Fonasa.`);
}