-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/production' into staging
- Loading branch information
Showing
5 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
app/api/externalIntegrations.v2/automaticTranslation/adapters/driving/ATServiceListener.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { tenants } from 'api/tenants'; | ||
import { TaskManager } from 'api/services/tasksmanager/TaskManager'; | ||
import { ATTranslationResultValidator } from '../../contracts/ATTranslationResultValidator'; | ||
import { AJVTranslationResultValidator } from '../../infrastructure/AJVTranslationResultValidator'; | ||
import { InvalidATServerResponse } from '../../errors/generateATErrors'; | ||
import { AutomaticTranslationFactory } from '../../AutomaticTranslationFactory'; | ||
|
||
export class ATServiceListener { | ||
static SERVICE_NAME = 'AutomaticTranslation'; | ||
|
||
private taskManager: TaskManager; | ||
|
||
constructor(ATFactory: typeof AutomaticTranslationFactory = AutomaticTranslationFactory) { | ||
const validator: ATTranslationResultValidator = new AJVTranslationResultValidator(); | ||
this.taskManager = new TaskManager({ | ||
serviceName: ATServiceListener.SERVICE_NAME, | ||
processResults: async result => { | ||
if (!validator.validate(result)) { | ||
throw new InvalidATServerResponse(validator.getErrors()[0]); | ||
} | ||
|
||
await tenants.run(async () => { | ||
await ATFactory.defaultSaveEntityTranslations().execute(result); | ||
}, result.key[0]); | ||
}, | ||
}); | ||
} | ||
|
||
start(interval = 500) { | ||
this.taskManager.subscribeToResults(interval); | ||
} | ||
|
||
async stop() { | ||
await this.taskManager.stop(); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
...rnalIntegrations.v2/automaticTranslation/adapters/driving/specs/ATServiceListener.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { config } from 'api/config'; | ||
import { tenants } from 'api/tenants'; | ||
import Redis from 'redis'; | ||
import RedisSMQ from 'rsmq'; | ||
import waitForExpect from 'wait-for-expect'; | ||
import { AutomaticTranslationFactory } from 'api/externalIntegrations.v2/automaticTranslation/AutomaticTranslationFactory'; | ||
import { testingEnvironment } from 'api/utils/testingEnvironment'; | ||
import { GenerateAutomaticTranslationsCofig } from 'api/externalIntegrations.v2/automaticTranslation/GenerateAutomaticTranslationConfig'; | ||
import { SaveEntityTranslations } from 'api/externalIntegrations.v2/automaticTranslation/SaveEntityTranslations'; | ||
import { ATServiceListener } from '../ATServiceListener'; | ||
|
||
const prepareATFactory = (executeSpy: jest.Mock<any, any, any>) => { | ||
const ATFactory: typeof AutomaticTranslationFactory = { | ||
defaultGenerateATConfig() { | ||
return {} as GenerateAutomaticTranslationsCofig; | ||
}, | ||
defaultSaveEntityTranslations() { | ||
return { execute: executeSpy } as unknown as SaveEntityTranslations; | ||
}, | ||
}; | ||
|
||
return ATFactory; | ||
}; | ||
|
||
describe('ATServiceListener', () => { | ||
let listener: ATServiceListener; | ||
let redisClient: Redis.RedisClient; | ||
let redisSMQ: RedisSMQ; | ||
let executeSpy: jest.Mock<any, any, any>; | ||
|
||
beforeEach(async () => { | ||
await testingEnvironment.setUp({ | ||
settings: [{ features: { automaticTranslation: { active: true } } }], | ||
}); | ||
await testingEnvironment.setTenant('tenant'); | ||
|
||
executeSpy = jest.fn(); | ||
|
||
listener = new ATServiceListener(prepareATFactory(executeSpy)); | ||
const redisUrl = `redis://${config.redis.host}:${config.redis.port}`; | ||
redisClient = Redis.createClient(redisUrl); | ||
redisSMQ = new RedisSMQ({ client: redisClient }); | ||
|
||
const recreateQueue = async (queueName: string): Promise<void> => { | ||
try { | ||
await redisSMQ.getQueueAttributesAsync({ qname: queueName }); | ||
await redisSMQ.deleteQueueAsync({ qname: queueName }); | ||
} catch (error: any) { | ||
if (error.name === 'queueNotFound') { | ||
// No action needed | ||
} else { | ||
throw error; | ||
} | ||
} | ||
|
||
await redisSMQ.createQueueAsync({ qname: queueName }); | ||
}; | ||
|
||
await recreateQueue('AutomaticTranslation_results').catch(error => { | ||
throw error; | ||
}); | ||
|
||
listener.start(0); | ||
}); | ||
|
||
afterAll(async () => { | ||
redisClient.end(true); | ||
await listener.stop(); | ||
await testingEnvironment.tearDown(); | ||
}); | ||
|
||
describe('Save Translations', () => { | ||
it('should call on saveEntityTranslations after validating the result', async () => { | ||
const message = { | ||
key: ['tenant', 'sharedId', 'propName'], | ||
text: 'original text', | ||
language_from: 'en', | ||
languages_to: ['es'], | ||
translations: [ | ||
{ text: 'texto traducido', language: 'es', success: true, error_message: '' }, | ||
], | ||
}; | ||
|
||
executeSpy.mockClear(); | ||
|
||
await redisSMQ.sendMessageAsync({ | ||
qname: 'AutomaticTranslation_results', | ||
message: JSON.stringify(message), | ||
}); | ||
|
||
await waitForExpect(async () => { | ||
await tenants.run(async () => { | ||
expect(executeSpy).toHaveBeenCalledWith(message); | ||
}, 'tenant'); | ||
}); | ||
}); | ||
}); | ||
}); |
1 change: 1 addition & 0 deletions
1
app/api/externalIntegrations.v2/automaticTranslation/errors/generateATErrors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/* eslint-disable max-classes-per-file */ | ||
export class GenerateATConfigError extends Error {} | ||
export class InvalidInputDataFormat extends Error {} | ||
export class InvalidATServerResponse extends Error {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters