613 lines
21 KiB
JavaScript
613 lines
21 KiB
JavaScript
// Test Examples for Device Bulk Operations with Images
|
|
// You can use these examples with Postman, fetch API, or any HTTP client
|
|
|
|
// =============================================================================
|
|
// Example 1: Bulk Create Devices with Images (JavaScript/Fetch)
|
|
// =============================================================================
|
|
async function bulkCreateDevicesWithImages() {
|
|
const formData = new FormData();
|
|
|
|
// Define devices
|
|
const devices = [
|
|
{
|
|
device_code: "ODP-BULK-001",
|
|
device_type: "ODP",
|
|
longitude: 106.8456,
|
|
latitude: -6.2088,
|
|
port_amount: 8,
|
|
status: "active",
|
|
province: "DKI Jakarta",
|
|
city: "Jakarta Selatan",
|
|
district: "Kebayoran Baru",
|
|
olt_id: "550e8400-e29b-41d4-a716-446655440000" // Optional: Replace with real OLT ID
|
|
},
|
|
{
|
|
device_code: "OTB-BULK-001",
|
|
device_type: "OTB",
|
|
longitude: 106.8556,
|
|
latitude: -6.2188,
|
|
port_amount: 16,
|
|
status: "active",
|
|
province: "DKI Jakarta",
|
|
city: "Jakarta Pusat",
|
|
tower_id: "550e8400-e29b-41d4-a716-446655440001" // Optional: Replace with real Tower ID
|
|
},
|
|
{
|
|
device_code: "CLOSURE-BULK-001",
|
|
device_type: "closure",
|
|
longitude: 106.8656,
|
|
latitude: -6.2288,
|
|
port_amount: 0,
|
|
status: "active",
|
|
province: "DKI Jakarta",
|
|
city: "Jakarta Utara"
|
|
}
|
|
];
|
|
|
|
formData.append('devices', JSON.stringify(devices));
|
|
|
|
// Image distribution:
|
|
// - Device 0 (ODP-BULK-001): 3 images
|
|
// - Device 1 (OTB-BULK-001): 2 images
|
|
// - Device 2 (CLOSURE-BULK-001): 1 image
|
|
formData.append('image_indexes', JSON.stringify([3, 2, 1]));
|
|
|
|
// Add images (total 6 files: 3+2+1)
|
|
// NOTE: Replace these with actual File objects from file input
|
|
// formData.append('images', odpImage1);
|
|
// formData.append('images', odpImage2);
|
|
// formData.append('images', odpImage3);
|
|
// formData.append('images', otbImage1);
|
|
// formData.append('images', otbImage2);
|
|
// formData.append('images', closureImage1);
|
|
|
|
try {
|
|
const response = await fetch('http://localhost:8080/device/bulk/create', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': 'Bearer YOUR_JWT_TOKEN_HERE'
|
|
},
|
|
body: formData
|
|
});
|
|
|
|
const result = await response.json();
|
|
console.log('Success:', result);
|
|
return result;
|
|
} catch (error) {
|
|
console.error('Error:', error);
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Example 2: Bulk Create without Images (Simple JSON)
|
|
// =============================================================================
|
|
async function bulkCreateDevicesNoImages() {
|
|
const requestBody = {
|
|
devices: [
|
|
{
|
|
device_code: "ODP-NO-IMG-001",
|
|
device_type: "ODP",
|
|
longitude: 106.8456,
|
|
latitude: -6.2088,
|
|
port_amount: 8,
|
|
status: "active",
|
|
province: "DKI Jakarta",
|
|
city: "Jakarta Selatan"
|
|
},
|
|
{
|
|
device_code: "ODP-NO-IMG-002",
|
|
device_type: "ODP",
|
|
longitude: 106.8556,
|
|
latitude: -6.2188,
|
|
port_amount: 12,
|
|
status: "active",
|
|
province: "DKI Jakarta",
|
|
city: "Jakarta Timur"
|
|
}
|
|
]
|
|
};
|
|
|
|
try {
|
|
const response = await fetch('http://localhost:8080/device/bulk/create', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': 'Bearer YOUR_JWT_TOKEN_HERE',
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(requestBody)
|
|
});
|
|
|
|
const result = await response.json();
|
|
console.log('Success:', result);
|
|
return result;
|
|
} catch (error) {
|
|
console.error('Error:', error);
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Example 3: Bulk Update Devices with Images (Append Mode)
|
|
// =============================================================================
|
|
async function bulkUpdateDevicesAppendImages() {
|
|
const formData = new FormData();
|
|
|
|
// Device IDs to update (replace with actual UUIDs from your database)
|
|
const deviceIds = [
|
|
"550e8400-e29b-41d4-a716-446655440010",
|
|
"550e8400-e29b-41d4-a716-446655440011"
|
|
];
|
|
|
|
formData.append('device_ids', JSON.stringify(deviceIds));
|
|
|
|
// Fields to update
|
|
const updates = {
|
|
status: "maintenance",
|
|
province: "DKI Jakarta"
|
|
};
|
|
formData.append('updates', JSON.stringify(updates));
|
|
|
|
// Image distribution:
|
|
// - Device 0: add 2 new images
|
|
// - Device 1: add 1 new image
|
|
formData.append('image_indexes', JSON.stringify([2, 1]));
|
|
|
|
// Append mode: keep existing images and add new ones
|
|
formData.append('replace_images', 'false');
|
|
|
|
// Add images (total 3 files: 2+1)
|
|
// NOTE: Replace these with actual File objects
|
|
// formData.append('images', newImage1);
|
|
// formData.append('images', newImage2);
|
|
// formData.append('images', newImage3);
|
|
|
|
try {
|
|
const response = await fetch('http://localhost:8080/device/bulk/update', {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Authorization': 'Bearer YOUR_JWT_TOKEN_HERE'
|
|
},
|
|
body: formData
|
|
});
|
|
|
|
const result = await response.json();
|
|
console.log('Success:', result);
|
|
return result;
|
|
} catch (error) {
|
|
console.error('Error:', error);
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Example 4: Bulk Update Devices with Images (Replace Mode)
|
|
// =============================================================================
|
|
async function bulkUpdateDevicesReplaceImages() {
|
|
const formData = new FormData();
|
|
|
|
const deviceIds = [
|
|
"550e8400-e29b-41d4-a716-446655440010"
|
|
];
|
|
|
|
formData.append('device_ids', JSON.stringify(deviceIds));
|
|
|
|
const updates = {
|
|
status: "active"
|
|
};
|
|
formData.append('updates', JSON.stringify(updates));
|
|
|
|
// Add 3 new images, replacing all old ones
|
|
formData.append('image_indexes', JSON.stringify([3]));
|
|
formData.append('replace_images', 'true'); // Replace mode
|
|
|
|
// Add images (total 3 files)
|
|
// formData.append('images', replacementImage1);
|
|
// formData.append('images', replacementImage2);
|
|
// formData.append('images', replacementImage3);
|
|
|
|
try {
|
|
const response = await fetch('http://localhost:8080/device/bulk/update', {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Authorization': 'Bearer YOUR_JWT_TOKEN_HERE'
|
|
},
|
|
body: formData
|
|
});
|
|
|
|
const result = await response.json();
|
|
console.log('Success:', result);
|
|
return result;
|
|
} catch (error) {
|
|
console.error('Error:', error);
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Example 5: Bulk Update without Images
|
|
// =============================================================================
|
|
async function bulkUpdateDevicesNoImages() {
|
|
const requestBody = {
|
|
device_ids: [
|
|
"550e8400-e29b-41d4-a716-446655440010",
|
|
"550e8400-e29b-41d4-a716-446655440011",
|
|
"550e8400-e29b-41d4-a716-446655440012"
|
|
],
|
|
updates: {
|
|
status: "inactive",
|
|
province: "Jawa Barat",
|
|
city: "Bandung"
|
|
}
|
|
};
|
|
|
|
try {
|
|
const response = await fetch('http://localhost:8080/device/bulk/update', {
|
|
method: 'PUT',
|
|
headers: {
|
|
'Authorization': 'Bearer YOUR_JWT_TOKEN_HERE',
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(requestBody)
|
|
});
|
|
|
|
const result = await response.json();
|
|
console.log('Success:', result);
|
|
return result;
|
|
} catch (error) {
|
|
console.error('Error:', error);
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Example 6: Bulk Delete Devices
|
|
// =============================================================================
|
|
async function bulkDeleteDevices() {
|
|
const requestBody = {
|
|
device_ids: [
|
|
"550e8400-e29b-41d4-a716-446655440010",
|
|
"550e8400-e29b-41d4-a716-446655440011"
|
|
]
|
|
};
|
|
|
|
try {
|
|
const response = await fetch('http://localhost:8080/device/bulk/delete', {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Authorization': 'Bearer YOUR_JWT_TOKEN_HERE',
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(requestBody)
|
|
});
|
|
|
|
const result = await response.json();
|
|
console.log('Success:', result);
|
|
return result;
|
|
} catch (error) {
|
|
console.error('Error:', error);
|
|
}
|
|
}
|
|
|
|
// =============================================================================
|
|
// Example 7: HTML Form for File Upload
|
|
// =============================================================================
|
|
/*
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Device Bulk Upload with Images</title>
|
|
</head>
|
|
<body>
|
|
<h1>Bulk Create Devices with Images</h1>
|
|
<form id="bulkCreateForm">
|
|
<div>
|
|
<label>Device 1 Code:</label>
|
|
<input type="text" id="device1_code" value="ODP-HTML-001" />
|
|
</div>
|
|
<div>
|
|
<label>Device 1 Images (select 2):</label>
|
|
<input type="file" id="device1_images" multiple accept="image/*" />
|
|
</div>
|
|
|
|
<div>
|
|
<label>Device 2 Code:</label>
|
|
<input type="text" id="device2_code" value="OTB-HTML-001" />
|
|
</div>
|
|
<div>
|
|
<label>Device 2 Images (select 1):</label>
|
|
<input type="file" id="device2_images" accept="image/*" />
|
|
</div>
|
|
|
|
<button type="submit">Upload</button>
|
|
</form>
|
|
|
|
<div id="result"></div>
|
|
|
|
<script>
|
|
document.getElementById('bulkCreateForm').addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
|
|
const formData = new FormData();
|
|
|
|
// Build devices array
|
|
const devices = [
|
|
{
|
|
device_code: document.getElementById('device1_code').value,
|
|
device_type: "ODP",
|
|
longitude: 106.8456,
|
|
latitude: -6.2088,
|
|
port_amount: 8,
|
|
status: "active"
|
|
},
|
|
{
|
|
device_code: document.getElementById('device2_code').value,
|
|
device_type: "OTB",
|
|
longitude: 106.8556,
|
|
latitude: -6.2188,
|
|
port_amount: 16,
|
|
status: "active"
|
|
}
|
|
];
|
|
|
|
formData.append('devices', JSON.stringify(devices));
|
|
|
|
// Get images
|
|
const device1Images = document.getElementById('device1_images').files;
|
|
const device2Images = document.getElementById('device2_images').files;
|
|
|
|
// Image distribution
|
|
formData.append('image_indexes', JSON.stringify([
|
|
device1Images.length,
|
|
device2Images.length
|
|
]));
|
|
|
|
// Add all images in order
|
|
for (let file of device1Images) {
|
|
formData.append('images', file);
|
|
}
|
|
for (let file of device2Images) {
|
|
formData.append('images', file);
|
|
}
|
|
|
|
try {
|
|
const response = await fetch('http://localhost:8080/device/bulk/create', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Authorization': 'Bearer YOUR_JWT_TOKEN_HERE'
|
|
},
|
|
body: formData
|
|
});
|
|
|
|
const result = await response.json();
|
|
document.getElementById('result').innerHTML =
|
|
'<pre>' + JSON.stringify(result, null, 2) + '</pre>';
|
|
} catch (error) {
|
|
document.getElementById('result').innerHTML =
|
|
'<p style="color:red">Error: ' + error.message + '</p>';
|
|
}
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|
|
*/
|
|
|
|
// =============================================================================
|
|
// cURL Examples
|
|
// =============================================================================
|
|
|
|
/*
|
|
# Bulk Create with Images
|
|
curl -X POST http://localhost:8080/device/bulk/create \
|
|
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
|
|
-F 'devices=[{"device_code":"ODP-CURL-001","device_type":"ODP","longitude":106.8,"latitude":-6.2,"port_amount":8,"status":"active"},{"device_code":"OTB-CURL-001","device_type":"OTB","longitude":106.9,"latitude":-6.3,"port_amount":16,"status":"active"}]' \
|
|
-F 'image_indexes=[2,1]' \
|
|
-F "images=@/path/to/image1.jpg" \
|
|
-F "images=@/path/to/image2.jpg" \
|
|
-F "images=@/path/to/image3.jpg"
|
|
|
|
# Bulk Create without Images (JSON)
|
|
curl -X POST http://localhost:8080/device/bulk/create \
|
|
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"devices":[{"device_code":"ODP-JSON-001","device_type":"ODP","longitude":106.8,"latitude":-6.2,"port_amount":8,"status":"active"}]}'
|
|
|
|
# Bulk Update with Images (Append)
|
|
curl -X PUT http://localhost:8080/device/bulk/update \
|
|
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
|
|
-F 'device_ids=["550e8400-e29b-41d4-a716-446655440010"]' \
|
|
-F 'updates={"status":"maintenance"}' \
|
|
-F 'image_indexes=[2]' \
|
|
-F 'replace_images=false' \
|
|
-F "images=@/path/to/new1.jpg" \
|
|
-F "images=@/path/to/new2.jpg"
|
|
|
|
# Bulk Update with Images (Replace)
|
|
curl -X PUT http://localhost:8080/device/bulk/update \
|
|
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
|
|
-F 'device_ids=["550e8400-e29b-41d4-a716-446655440010"]' \
|
|
-F 'updates={"status":"active"}' \
|
|
-F 'image_indexes=[3]' \
|
|
-F 'replace_images=true' \
|
|
-F "images=@/path/to/replace1.jpg" \
|
|
-F "images=@/path/to/replace2.jpg" \
|
|
-F "images=@/path/to/replace3.jpg"
|
|
|
|
# Bulk Update without Images (JSON)
|
|
curl -X PUT http://localhost:8080/device/bulk/update \
|
|
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"device_ids":["550e8400-e29b-41d4-a716-446655440010","550e8400-e29b-41d4-a716-446655440011"],"updates":{"status":"inactive"}}'
|
|
|
|
# Bulk Delete
|
|
curl -X DELETE http://localhost:8080/device/bulk/delete \
|
|
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
|
|
-H "Content-Type: application/json" \
|
|
-d '{"device_ids":["550e8400-e29b-41d4-a716-446655440010"]}'
|
|
*/
|
|
|
|
// =============================================================================
|
|
// Postman Collection JSON (Import this into Postman)
|
|
// =============================================================================
|
|
/*
|
|
{
|
|
"info": {
|
|
"name": "Device Bulk Operations with Images",
|
|
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
|
|
},
|
|
"item": [
|
|
{
|
|
"name": "Bulk Create with Images",
|
|
"request": {
|
|
"method": "POST",
|
|
"header": [
|
|
{
|
|
"key": "Authorization",
|
|
"value": "Bearer {{jwt_token}}"
|
|
}
|
|
],
|
|
"body": {
|
|
"mode": "formdata",
|
|
"formdata": [
|
|
{
|
|
"key": "devices",
|
|
"value": "[{\"device_code\":\"ODP-PM-001\",\"device_type\":\"ODP\",\"longitude\":106.8,\"latitude\":-6.2,\"port_amount\":8,\"status\":\"active\"}]",
|
|
"type": "text"
|
|
},
|
|
{
|
|
"key": "image_indexes",
|
|
"value": "[2]",
|
|
"type": "text"
|
|
},
|
|
{
|
|
"key": "images",
|
|
"type": "file",
|
|
"src": "/path/to/image1.jpg"
|
|
},
|
|
{
|
|
"key": "images",
|
|
"type": "file",
|
|
"src": "/path/to/image2.jpg"
|
|
}
|
|
]
|
|
},
|
|
"url": {
|
|
"raw": "http://localhost:8080/device/bulk/create",
|
|
"protocol": "http",
|
|
"host": ["localhost"],
|
|
"port": "8080",
|
|
"path": ["device", "bulk", "create"]
|
|
}
|
|
}
|
|
},
|
|
{
|
|
"name": "Bulk Update with Images (Append)",
|
|
"request": {
|
|
"method": "PUT",
|
|
"header": [
|
|
{
|
|
"key": "Authorization",
|
|
"value": "Bearer {{jwt_token}}"
|
|
}
|
|
],
|
|
"body": {
|
|
"mode": "formdata",
|
|
"formdata": [
|
|
{
|
|
"key": "device_ids",
|
|
"value": "[\"550e8400-e29b-41d4-a716-446655440010\"]",
|
|
"type": "text"
|
|
},
|
|
{
|
|
"key": "updates",
|
|
"value": "{\"status\":\"maintenance\"}",
|
|
"type": "text"
|
|
},
|
|
{
|
|
"key": "image_indexes",
|
|
"value": "[1]",
|
|
"type": "text"
|
|
},
|
|
{
|
|
"key": "replace_images",
|
|
"value": "false",
|
|
"type": "text"
|
|
},
|
|
{
|
|
"key": "images",
|
|
"type": "file",
|
|
"src": "/path/to/new_image.jpg"
|
|
}
|
|
]
|
|
},
|
|
"url": {
|
|
"raw": "http://localhost:8080/device/bulk/update",
|
|
"protocol": "http",
|
|
"host": ["localhost"],
|
|
"port": "8080",
|
|
"path": ["device", "bulk", "update"]
|
|
}
|
|
}
|
|
}
|
|
]
|
|
}
|
|
*/
|
|
|
|
// =============================================================================
|
|
// Expected Response Examples
|
|
// =============================================================================
|
|
|
|
/*
|
|
// Success Response
|
|
{
|
|
"message": "Bulk create completed: 2 successful, 0 failed out of 2 requested (with 3 images)",
|
|
"data": {
|
|
"total_requested": 2,
|
|
"successful": 2,
|
|
"failed": 0,
|
|
"errors": [],
|
|
"results": [
|
|
{
|
|
"id": "550e8400-e29b-41d4-a716-446655440100",
|
|
"device_code": "ODP-BULK-001",
|
|
"device_type": "ODP",
|
|
"longitude": 106.8456,
|
|
"latitude": -6.2088,
|
|
"port_amount": 8,
|
|
"status": "active",
|
|
"address": "Jakarta Selatan, DKI Jakarta",
|
|
"image_url": "/uploads/devices/abc123_1728000000.jpg",
|
|
"image_urls": [
|
|
"/uploads/devices/abc123_1728000000.jpg",
|
|
"/uploads/devices/def456_1728000001.jpg"
|
|
],
|
|
"created_at": "2025-10-10T10:30:00Z",
|
|
"updated_at": "2025-10-10T10:30:00Z"
|
|
}
|
|
],
|
|
"execution_time": "245ms"
|
|
}
|
|
}
|
|
|
|
// Partial Failure Response
|
|
{
|
|
"message": "Bulk create completed: 1 successful, 1 failed out of 2 requested (with 2 images)",
|
|
"data": {
|
|
"total_requested": 2,
|
|
"successful": 1,
|
|
"failed": 1,
|
|
"errors": [
|
|
{
|
|
"index": 1,
|
|
"error": "Tower not found",
|
|
"details": "550e8400-e29b-41d4-a716-446655440001"
|
|
}
|
|
],
|
|
"results": [
|
|
{
|
|
"id": "550e8400-e29b-41d4-a716-446655440100",
|
|
"device_code": "ODP-BULK-001",
|
|
"image_url": "/uploads/devices/abc123.jpg",
|
|
"image_urls": ["/uploads/devices/abc123.jpg"]
|
|
}
|
|
],
|
|
"execution_time": "180ms"
|
|
}
|
|
}
|
|
*/
|