tango-app-api-audio-analytics 1.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tango-app-api-audio-analytics",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "audioAnalytics",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -1,4 +1,4 @@
1
- import { insertWithId, logger } from 'tango-app-api-middleware';
1
+ import { insertWithId, updateOpenSearchData, searchOpenSearchData, logger } from 'tango-app-api-middleware';
2
2
  import { randomUUID } from 'crypto';
3
3
 
4
4
  const EXTERNAL_STREAM_API = 'http://172.236.179.51:8000/stream';
@@ -44,6 +44,110 @@ export async function createCohort( req, res ) {
44
44
  }
45
45
  }
46
46
 
47
+ export async function updateCohort( req, res ) {
48
+ try {
49
+ const { cohortId, ...updateFields } = req.body;
50
+
51
+ const metrics = updateFields.metrics ?
52
+ updateFields.metrics.map( ( metric ) => ( {
53
+ ...metric,
54
+ metricId: metric.metricId || randomUUID(),
55
+ hasContext: Array.isArray( metric.contexts ) && metric.contexts.length > 0,
56
+ isNumeric: metric.isNumeric ?? false,
57
+ } ) ) :
58
+ undefined;
59
+
60
+ const updateData = {
61
+ ...( updateFields.cohortName && { cohortName: updateFields.cohortName } ),
62
+ ...( updateFields.cohortDescription && { cohortDescription: updateFields.cohortDescription } ),
63
+ ...( updateFields.source && { source: updateFields.source } ),
64
+ ...( metrics && { metrics } ),
65
+ updatedAt: new Date().toISOString(),
66
+ };
67
+
68
+ const result = await updateOpenSearchData( 'tango-audio-config', cohortId, updateData );
69
+ logger.info( { result } );
70
+
71
+ if ( result && result.body && result.body.result === 'updated' ) {
72
+ return res.sendSuccess( { result: 'Cohort updated successfully', cohortId } );
73
+ } else {
74
+ return res.sendError( 'Failed to update cohort', 500 );
75
+ }
76
+ } catch ( error ) {
77
+ const err = error.message || 'Internal Server Error';
78
+ logger.error( { error, message: req.body, function: 'updateCohort' } );
79
+ return res.sendError( err, 500 );
80
+ }
81
+ }
82
+
83
+ export async function getCohort( req, res ) {
84
+ try {
85
+ const { cohortId } = req.params;
86
+
87
+ const query = {
88
+ query: {
89
+ match: { cohortId },
90
+ },
91
+ };
92
+
93
+ const result = await searchOpenSearchData( 'tango-audio-config', query );
94
+ logger.info( { result } );
95
+
96
+ if ( !result || result?.hits?.hits?.length === 0 ) {
97
+ return res.sendError( 'Cohort not found', 404 );
98
+ }
99
+
100
+ return res.sendSuccess( result?.hits?.hits?.[0]?._source );
101
+ } catch ( error ) {
102
+ const err = error.message || 'Internal Server Error';
103
+ logger.error( { error, message: req.params, function: 'getCohort' } );
104
+ return res.sendError( err, 500 );
105
+ }
106
+ }
107
+
108
+ export async function listCohortsByClient( req, res ) {
109
+ try {
110
+ const { clientId, limit = 10, offset = 0 } = req.query;
111
+ logger.info( { message: req.query, function: 'listCohortsByClient' } );
112
+ const query = {
113
+ query: {
114
+ match: { clientId },
115
+ },
116
+ size: Number( limit ),
117
+ from: Number( offset ),
118
+ sort: [ { createdAt: { order: 'desc' } } ],
119
+ };
120
+
121
+ const result = await searchOpenSearchData( 'tango-audio-config', query );
122
+ logger.info( { result } );
123
+
124
+ const total = result?.body?.hits?.total?.value || 0;
125
+ const cohorts = ( result?.body?.hits?.hits || [] ).map( ( hit ) => {
126
+ const source = hit._source;
127
+ return {
128
+ cohortId: source.cohortId,
129
+ cohortName: source.cohortName,
130
+ cohortDescription: source.cohortDescription,
131
+ metricsAdded: source.metrics?.length || 0,
132
+ };
133
+ } );
134
+
135
+ return res.sendSuccess( {
136
+ cohorts,
137
+ pagination: {
138
+ total,
139
+ limit: Number( limit ),
140
+ offset: Number( offset ),
141
+ hasMore: Number( offset ) + Number( limit ) < total,
142
+ },
143
+ } );
144
+ } catch ( error ) {
145
+ const err = error.message || 'Internal Server Error';
146
+ logger.error( { error, message: req.query, function: 'listCohortsByClient' } );
147
+ return res.sendError( err, 500 );
148
+ }
149
+ }
150
+
47
151
  export const callExternalStreamAPI = async ( req, res ) => {
48
152
  try {
49
153
  const { prompt, storeId, productModule, country, region, fromDate, toDate, clusters } = req.body;
@@ -536,3 +536,33 @@ export const createCohortSchema = joi.object( {
536
536
  export const createCohortValid = {
537
537
  body: createCohortSchema,
538
538
  };
539
+
540
+ const updateCohortSchema = joi.object( {
541
+ cohortId: joi.string().required(),
542
+ cohortName: joi.string().optional(),
543
+ cohortDescription: joi.string().optional(),
544
+ source: joi.array().optional(),
545
+ metrics: joi.array().items( cohortMetricSchema ).optional(),
546
+ } );
547
+
548
+ export const updateCohortValid = {
549
+ body: updateCohortSchema,
550
+ };
551
+
552
+ const getCohortSchema = joi.object( {
553
+ cohortId: joi.string().required(),
554
+ } );
555
+
556
+ export const getCohortValid = {
557
+ params: getCohortSchema,
558
+ };
559
+
560
+ const listCohortsByClientSchema = joi.object( {
561
+ clientId: joi.string().required(),
562
+ limit: joi.number().integer().min( 1 ).max( 100 ).optional().default( 10 ),
563
+ offset: joi.number().integer().min( 0 ).optional().default( 0 ),
564
+ } );
565
+
566
+ export const listCohortsByClientValid = {
567
+ query: listCohortsByClientSchema,
568
+ };
@@ -1,8 +1,8 @@
1
1
 
2
2
  import express from 'express';
3
3
  import { validate } from 'tango-app-api-middleware';
4
- import { createCohortValid } from '../dtos/audioAnalytics.dtos.js';
5
- import { createCohort } from '../controllers/audioAnalytics.controller.js';
4
+ import { createCohortValid, updateCohortValid, getCohortValid, listCohortsByClientValid } from '../dtos/audioAnalytics.dtos.js';
5
+ import { createCohort, updateCohort, getCohort, listCohortsByClient } from '../controllers/audioAnalytics.controller.js';
6
6
 
7
7
  export const audioAnalyticsrouter = express.Router(); ;
8
8
 
@@ -13,6 +13,9 @@ audioAnalyticsrouter.get( '/test', ( req, res ) => {
13
13
  } );
14
14
 
15
15
  audioAnalyticsrouter.post( '/create-cohort', validate( createCohortValid ), createCohort );
16
+ audioAnalyticsrouter.post( '/update-cohort', validate( updateCohortValid ), updateCohort );
17
+ audioAnalyticsrouter.get( '/get-cohort/:cohortId', validate( getCohortValid ), getCohort );
18
+ audioAnalyticsrouter.get( '/list-cohorts', validate( listCohortsByClientValid ), listCohortsByClient );
16
19
 
17
20
 
18
21
  export default audioAnalyticsrouter;