Spaces:
Paused
Paused
Download src/group/group.controller.ts from Rasyiq03/scheffin-api: direct link, hf CLI and curl.
- Browser
- Download file 2.42 kB
-
https://huggingface.co/spaces/Rasyiq03/scheffin-api/resolve/main/src/group/group.controller.ts
- Command line
-
hf download hf://spaces/Rasyiq03/scheffin-api/src/group/group.controller.ts
-
curl -L -o group.controller.ts https://huggingface.co/spaces/Rasyiq03/scheffin-api/resolve/main/src/group/group.controller.ts
2.42 kB
| import { Controller, Get, Res, HttpStatus, Query } from '@nestjs/common'; | |
| import { WhatsappService } from '../whatsapp/whatsapp.service'; | |
| ('get/groups') | |
| export class GroupController { | |
| constructor(private readonly whatsappService: WhatsappService) {} | |
| ('/') | |
| async getGroups(() res: any) { | |
| try { | |
| const sock = this.whatsappService.getSocket(); | |
| const groups = await sock.groupFetchAllParticipating(); | |
| const formattedGroups = Object.values(groups).map((g) => ({ | |
| name: g.subject, | |
| jid: g.id, | |
| participants: g.participants.length, | |
| })); | |
| // Kirim respons sukses | |
| return res.status(HttpStatus.OK).json({ | |
| status: 'Success', | |
| data: formattedGroups, | |
| }); | |
| } catch (err: unknown) { | |
| const message = err instanceof Error ? err.message : 'Unknown error'; | |
| console.error('Gagal mengambil data grup:', message); | |
| const cachedGroups = this.whatsappService.getAllGroupsCache(); | |
| if (cachedGroups.length > 0) { | |
| return res.status(HttpStatus.OK).json({ | |
| status: 'Success (Cached)', | |
| data: cachedGroups, | |
| }); | |
| } | |
| return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ | |
| status: 'Error', | |
| message: 'Gagal mengambil grup dan cache kosong: ' + message, | |
| }); | |
| } | |
| } | |
| ('id') | |
| async getGroupId( | |
| ('name') name: string, | |
| ('jid') jid: string, | |
| () res: any, | |
| ) { | |
| try { | |
| if (jid) { | |
| return res.status(HttpStatus.OK).json({ status: 'Success', data: { jid } }); | |
| } | |
| if (!name) { | |
| return res.status(HttpStatus.BAD_REQUEST).json({ status: 'Error', message: 'Query `name` atau `jid` diperlukan.' }); | |
| } | |
| const cached = this.whatsappService.getAllGroupsCache(); | |
| const matches = cached.filter((g) => g.name && g.name.toLowerCase().includes(name.toLowerCase())); | |
| if (matches.length > 0) { | |
| return res.status(HttpStatus.OK).json({ status: 'Success', data: matches }); | |
| } | |
| return res.status(HttpStatus.NOT_FOUND).json({ status: 'NotFound', message: 'Tidak ditemukan grup yang cocok.' }); | |
| } catch (err: unknown) { | |
| const message = err instanceof Error ? err.message : 'Unknown error'; | |
| console.error('Gagal mencari ID grup:', message); | |
| return res.status(HttpStatus.INTERNAL_SERVER_ERROR).json({ status: 'Error', message }); | |
| } | |
| } | |
| } | |