Skip to content

Commit

Permalink
wip: crowdin api integration
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeKarow committed Apr 26, 2024
1 parent 80b755a commit 828522b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { addSingleKeyFromNestedFreetextCreate } from '@weareinreach/crowdin/api'
import { generateNestedFreeText, getAuditedClient } from '@weareinreach/db'
import { connectOneId, connectOneIdRequired } from '~api/schemas/nestedOps'
import { type TRPCHandlerParams } from '~api/types/handler'
Expand All @@ -22,25 +23,31 @@ const attachAttribute = async ({ ctx, input }: TRPCHandlerParams<TAttachAttribut
const freeText = input.text
? generateNestedFreeText({ orgId, text: input.text, type: 'attSupp', itemId: input.id })
: undefined

const result = await prisma.attributeSupplement.create({
data: {
id: input.id,
attribute: connectOneIdRequired(input.attributeId),
organization: connectOneId(organizationId),
country: connectOneId(input.countryId),
govDist: connectOneId(input.govDistId),
language: connectOneId(input.languageId),
service: connectOneId(serviceId),
location: connectOneId(locationId),
boolean: input.boolean,
data: input.data,
text: freeText,
},
select: {
id: true,
},
const batchedUpdate = await prisma.$transaction(async (tx) => {
if (freeText) {
const { id: crowdinId } = await addSingleKeyFromNestedFreetextCreate(freeText)
freeText.create.tsKey.create.crowdinId = crowdinId
}
const result = await tx.attributeSupplement.create({
data: {
id: input.id,
attribute: connectOneIdRequired(input.attributeId),
organization: connectOneId(organizationId),
country: connectOneId(input.countryId),
govDist: connectOneId(input.govDistId),
language: connectOneId(input.languageId),
service: connectOneId(serviceId),
location: connectOneId(locationId),
boolean: input.boolean,
data: input.data,
text: freeText,
},
select: {
id: true,
},
})
return result
})
return result
return batchedUpdate
}
export default attachAttribute
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { addSingleKeyFromNestedFreetextCreate } from '@weareinreach/crowdin/api'
import { getAuditedClient } from '@weareinreach/db'
import { type TRPCHandlerParams } from '~api/types/handler'

Expand All @@ -6,8 +7,15 @@ import { type TCreateNewQuickSchema } from './mutation.createNewQuick.schema'
const createNewQuick = async ({ ctx, input }: TRPCHandlerParams<TCreateNewQuickSchema, 'protected'>) => {
const prisma = getAuditedClient(ctx.actorId)

const result = await prisma.organization.create(input)
const batchedResult = await prisma.$transaction(async (tx) => {
if (input.data.description) {
const { id: crowdinId } = await addSingleKeyFromNestedFreetextCreate(input.data.description)
input.data.description.create.tsKey.create.crowdinId = crowdinId
}
const result = await tx.organization.create(input)

return result
return result
})
return batchedResult
}
export default createNewQuick
59 changes: 31 additions & 28 deletions packages/api/router/organization/mutation.updateBasic.handler.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import { crowdinApi, getStringIdByKey, projectId } from '@weareinreach/crowdin/api'
import { addSingleKey, updateSingleKey } from '@weareinreach/crowdin/api'
import {
generateFreeText,
generateId,
generateNestedFreeTextUpsert,
generateUniqueSlug,
getAuditedClient,
type Prisma,
} from '@weareinreach/db'
import { isVercelProd } from '@weareinreach/env'
import { createLoggerInstance } from '@weareinreach/util/logger'
import { handleError } from '~api/lib/errorHandler'
import { type TRPCHandlerParams } from '~api/types/handler'

import { type TUpdateBasicSchema } from './mutation.updateBasic.schema'

const logger = createLoggerInstance('api - organization.updateBasic')
const updateBasic = async ({ ctx, input }: TRPCHandlerParams<TUpdateBasicSchema, 'protected'>) => {
try {
const prisma = getAuditedClient(ctx.actorId)
Expand All @@ -30,14 +27,36 @@ const updateBasic = async ({ ctx, input }: TRPCHandlerParams<TUpdateBasicSchema,
data.oldSlugs = { create: { from: existing.slug, to: newSlug, id: generateId('slugRedirect') } }
}
if (input.description) {
// TODO: [IN-920] Handle new string creation in Crowdin
const newText = generateFreeText({ orgId: input.id, type: 'orgDesc', text: input.description })
data.description = {
upsert: {
update: { tsKey: { update: { text: input.description } } },
create: { id: newText.freeText.id, tsKey: { create: newText.translationKey } },
},
const upsertDescription = generateNestedFreeTextUpsert({
orgId: input.id,
type: 'orgDesc',
text: input.description,
})
if (existing.description?.tsKey.crowdinId) {
console.log('update crowdin', {
key: existing.description.tsKey.key,
isDatabaseString: true,
updatedString: upsertDescription.upsert.update.tsKey.update.text,
})
await updateSingleKey({
key: existing.description.tsKey.key,
isDatabaseString: true,
updatedString: upsertDescription.upsert.update.tsKey.update.text,
})
} else {
console.log('add crowdin', {
isDatabaseString: true,
key: upsertDescription.upsert.create.tsKey.create.key,
text: upsertDescription.upsert.create.tsKey.create.text,
})
const { id: crowdinId } = await addSingleKey({
isDatabaseString: true,
key: upsertDescription.upsert.create.tsKey.create.key,
text: upsertDescription.upsert.create.tsKey.create.text,
})
upsertDescription.upsert.create.tsKey.create.crowdinId = crowdinId
}
data.description = upsertDescription
}
const update = await prisma.organization.update({
where: { id: input.id },
Expand All @@ -47,22 +66,6 @@ const updateBasic = async ({ ctx, input }: TRPCHandlerParams<TUpdateBasicSchema,
},
data,
})
if (update && input.description && existing.description) {
const stringId =
existing.description.tsKey.crowdinId ??
(await getStringIdByKey(existing.description?.tsKey?.key, true))
if (stringId) {
if (isVercelProd) {
await crowdinApi.sourceStringsApi.editString(projectId, stringId, [
{ op: 'replace', path: '/text', value: input.description },
])
} else {
logger.info(
`\n==========\nSkipping Crowdin Update - Not production environment.\nCrowdin String ID: ${stringId}. Updated Description: ${input.description}\n==========`
)
}
}
}
return update
} catch (error) {
return handleError(error)
Expand Down

0 comments on commit 828522b

Please sign in to comment.