Fix the dup key issue on i18n name (#85)

This commit is contained in:
Niklas Fondberg
2021-11-10 11:38:41 +01:00
committed by GitHub
parent 0afe689200
commit a979d30314
3 changed files with 27 additions and 14 deletions
+1 -3
View File
@@ -828,9 +828,7 @@ export class ProductAPI extends BaseSQLDataSource {
WHERE ppf.value IN (${whereClause});
`;
const res = await this.cachedRaw(query)
.cache(MINUTE)
.then((data) => data.rows);
const res = await this.knex.raw(query).then((data) => data.rows);
if (!res.length) {
throw new UserInputError('No articles found');
+24 -9
View File
@@ -14,9 +14,13 @@ export class TextsAPI extends SQLDataSource {
this.marketLocaleApi = marketLocaleApi;
}
async translateProductName(name: string, oldPath, newPath): Promise<void> {
// Check if we have an entry already
const textKey = await this.upsertTextId(newPath, oldPath);
async translateProductName(
name: string,
oldPath: string,
newPath: string,
): Promise<void> {
// Check if we have an entry already, if so update it
const textId = await this.upsertTextId(newPath, oldPath);
const locales = await this.marketLocaleApi.getLocales();
for (let i = 0; i < locales.length; i++) {
@@ -31,7 +35,7 @@ export class TextsAPI extends SQLDataSource {
};
const translated = await translateText(input);
await this.upsertTextDataEntry(textKey.textid, translated, locales[i].id);
await this.upsertTextDataEntry(textId, translated, locales[i].id);
}
}
@@ -73,17 +77,28 @@ export class TextsAPI extends SQLDataSource {
}
/**
* Upsert translated entry to the i18n-texts table
* Upsert translated entry to the i18n-texts table returning the entry (textId)
* @param newKey the new name of the text key
* @param localeId the old name of the text key
*/
private async upsertTextId(newKey: string, oldKey: string) {
private async upsertTextId(newKey: string, oldKey: string): Promise<number> {
let textEntry = await this.knex
.select('*')
.from('i18n-texts')
.where({ name: oldKey })
.where({ name: oldKey, category: 'productTitles' })
.first();
const textNewEntry = await this.knex
.select('*')
.from('i18n-texts')
.where({ name: newKey, category: 'productTitles' })
.first();
// New key did exist, which is kind of wrong but return it for updating the name
if (textNewEntry?.textid) {
return textNewEntry.textid;
}
let textId = textEntry?.textid;
// No entry, create one
if (!textId) {
@@ -99,8 +114,8 @@ export class TextsAPI extends SQLDataSource {
await this.knex
.table('i18n-texts')
.update({ name: newKey })
.where({ name: oldKey });
.where({ name: oldKey, category: 'productTitles' });
}
return textEntry;
return textEntry.textid;
}
}
+2 -2
View File
@@ -15,7 +15,7 @@ import { MarketLocaleAPI } from './datasources/market-locale-api';
import { ImageServerApi } from './datasources/imageserver-api';
import { InteriorAPI } from './datasources/interior-api';
import { ClaimVerifyResult, verifyToken } from './cognito/cognito-client';
import { verifyToken } from './cognito/cognito-client';
import { readFileSync } from 'fs';
import { InteriorsLambdaAPI } from './datasources/interiors-lambda-api';
import { TextsAPI } from './datasources/texts-api';
@@ -24,7 +24,7 @@ const typeDefs = readFileSync(
path.join(__dirname, './schema.graphql'),
).toString('utf-8');
// Should we convert columns?
// Converts column names to CamelCase
const knexConfig = knexStringcase(dbConfig);
// set up any dataSources our resolvers need