This commit is contained in:
zramsay 2025-01-24 10:37:14 -05:00
parent a8666c94e3
commit 5f2cd718a8

View File

@ -1,16 +1,11 @@
// src/services/laconicService.ts
import { promises as fs } from 'fs' import { promises as fs } from 'fs'
import { exec } from 'child_process'
import { promisify } from 'util'
import yaml from 'yaml' import yaml from 'yaml'
import axios from 'axios'
const execAsync = promisify(exec)
interface LaconicAnimalRecord { interface LaconicAnimalRecord {
record: { record: {
type: 'AnimalRecord' type: 'AnimalRecord'
mainObject: string // changed from species mainObject: string
location: { location: {
latitude: number latitude: number
longitude: number longitude: number
@ -22,7 +17,7 @@ interface LaconicAnimalRecord {
} }
export async function publishAnimalRecord( export async function publishAnimalRecord(
mainObject: string, // changed parameter name mainObject: string,
latitude: number, latitude: number,
longitude: number, longitude: number,
description: string, description: string,
@ -30,14 +25,10 @@ export async function publishAnimalRecord(
portalName: string portalName: string
): Promise<string> { ): Promise<string> {
try { try {
// Verify config file exists
await fs.access('config.yml')
// Create animal record
const record: LaconicAnimalRecord = { const record: LaconicAnimalRecord = {
record: { record: {
type: 'AnimalRecord', type: 'AnimalRecord',
mainObject, // use mainObject instead of species mainObject,
location: { location: {
latitude, latitude,
longitude longitude
@ -48,25 +39,17 @@ export async function publishAnimalRecord(
} }
} }
// Write temporary record file const response = await axios.post('http://143.198.37.25:3000/publishRecord', {
const recordPath = 'animal-record.yml' yamlContent: yaml.stringify(record)
await fs.writeFile(recordPath, yaml.stringify(record)) }, {
headers: {
try { 'Authorization': `Bearer 1234`, //${process.env.LACONIC_AUTH_TOKEN}`,
// Execute laconic command 'Content-Type': 'application/json'
const { stdout, stderr } = await execAsync(
`laconic -c config.yml registry record publish --filename animal-record.yml --user-key "${process.env.LACONIC_USER_KEY}" --bond-id "${process.env.LACONIC_BOND_ID}"`
)
if (stderr) {
console.error('Laconic command stderr:', stderr)
} }
})
return response.data.output
return stdout.trim()
} finally {
// Clean up temporary file
await fs.unlink(recordPath).catch(console.error)
}
} catch (error) { } catch (error) {
console.error('Failed to publish animal record:', error) console.error('Failed to publish animal record:', error)
throw error throw error