Koleksi devices menyimpan data device milik user. Diupdate oleh engine saat koneksi WhatsApp berubah (open/close).
Model
src/model/devices.model.ts:
const DeviceSchema = new Schema<DeviceCollection>({
id: { type: String, required: true, index: true },
iduser: { type: String, required: true },
store_id: { type: String, required: true },
token: { type: String, required: true },
no_wa: { type: String, required: true },
is_connected: { type: String, required: true },
updated_at: { type: Date },
});
export const DeviceModel = model<DeviceCollection>(
'devices',
DeviceSchema,
'devices', // nama koleksi eksplisit
);Field
| Field | Tipe | Wajib | Index | Catatan |
|---|---|---|---|---|
id | String | ✅ | ✅ | UUID = instanceId — primary key logis |
iduser | String | ✅ | — | ID user pemilik device |
store_id | String | ✅ | — | ID store/toko — dipakai untuk chat_histories lookup |
token | String | ✅ | — | Token device (untuk messaging API) |
no_wa | String | ✅ | — | Nomor WhatsApp (tanpa @s.whatsapp.net) |
is_connected | String | ✅ | — | Status koneksi: "Connected" / "Disconnected" (string, bukan boolean) |
updated_at | Date | ❌ | — | Timestamp update terakhir |
_id | ObjectId | auto | — | Mongo default (tidak dipakai di query bisnis) |
Cara engine memakai
Update saat koneksi berubah
src/service/whatsappService.ts:170-207 — di handler connection.update:
// Saat connection === 'open'
let deviceMember = await DeviceService.findOne(instanceId);
if (!deviceMember) {
// Fallback ke admin_devices
await DeviceService.updateDeviceAdmin(instanceId, {
no_wa: phoneNumber,
is_connected: 'Connected',
status: 1,
});
} else {
await DeviceService.update(instanceId, {
no_wa: phoneNumber,
is_connected: 'Connected',
});
}
// Saat connection === 'close' (shouldReconnect)
await DeviceService.update(instanceId, {
no_wa: '', // kosong
is_connected: 'Disconnected',
});Lookup untuk chat_histories
src/service/chatHistory/chatHistoryService.ts:30-37:
const device = await DeviceModel.findOne({ id: instanceId });
if (!device) return; // skip bila device tidak ada
// Pakai device.store_id untuk filter chat_historiesService — DeviceService
src/service/devices/deviceServices.ts:
| Method | Fungsi |
|---|---|
findOne(instanceId) | Cari device by id — return null jika tidak ada |
update(instanceId, data) | findOneAndUpdate by id, $set data |
updateDeviceAdmin(instanceId, data) | Fallback — update ke admin_devices |
Shared dengan
| Service | Akses |
|---|---|
dazoapp (Laravel) | Writer utama — create device, set store_id, token, iduser |
backend-go | Read/write |
engine-whatsapp | Update no_wa, is_connected saat koneksi berubah |
Contoh dokumen
{
"_id": "65a1b2c3d4e5f6a7b8c9d0e1",
"id": "0c1a968c-7918-4c29-a524-0ad7c445ee7f",
"iduser": "user-123",
"store_id": "store-456",
"token": "device-token-xxx",
"no_wa": "628123456789",
"is_connected": "Connected",
"updated_at": "2024-01-15T10:30:00.000Z",
"__v": 0
}Langkah berikutnya
admin_devices(fallback)? Baca admin_devices.chat_inbox? Baca chat_inbox.chat_histories? Baca chat_histories.