refactor: streamline Redis caching logic across services
- Implemented a unified `getAndSet` method in RedisService to handle caching for single, list, and paginated responses. - Removed redundant cache checks and writes in various services, simplifying the code and improving readability. - Updated GoodsService, StockKeepingUnitsService, PartnerActivatedLicensesService, and others to utilize the new caching mechanism. - Adjusted Prisma connection limit for better resource management.
This commit is contained in:
@@ -1,17 +1,162 @@
|
||||
# AGENT.md
|
||||
# GapCode Agent Instructions
|
||||
|
||||
# AGENTS.md
|
||||
|
||||
Operational guide for AI/coding agents working in `consumer_api`.
|
||||
|
||||
---
|
||||
|
||||
# ⚡ RTK LOW-TOKEN EXECUTION POLICY (MANDATORY)
|
||||
|
||||
This repository uses **RTK (Rust Token Killer)**.
|
||||
|
||||
All shell interactions MUST prefer RTK wrappers to reduce token usage.
|
||||
|
||||
If an RTK equivalent exists, **never use the raw command**.
|
||||
|
||||
This repository can be large; uncontrolled shell output will quickly exhaust model context.
|
||||
|
||||
---
|
||||
|
||||
# ✅ COMMAND REWRITE RULES (STRICT)
|
||||
|
||||
Always rewrite commands using RTK:
|
||||
|
||||
git status → rtk git status
|
||||
git diff → rtk git diff
|
||||
git log → rtk git log
|
||||
ls → rtk ls
|
||||
tree → rtk ls
|
||||
cat <file> → rtk read <file>
|
||||
grep <pattern> → rtk grep <pattern>
|
||||
rg <pattern> → rtk grep <pattern>
|
||||
find → rtk find
|
||||
|
||||
Never run:
|
||||
|
||||
- raw git commands for inspection
|
||||
- raw cat
|
||||
- raw grep
|
||||
- raw ls
|
||||
- raw tree
|
||||
|
||||
---
|
||||
|
||||
# 📂 CODE NAVIGATION WORKFLOW (MANDATORY)
|
||||
|
||||
When working in this repository follow this order:
|
||||
|
||||
1️⃣ Discover structure
|
||||
|
||||
rtk ls
|
||||
|
||||
2️⃣ Search before opening files
|
||||
|
||||
rtk grep <symbol | class | function | DTO | service>
|
||||
|
||||
3️⃣ Read only the necessary files
|
||||
|
||||
rtk read <file>
|
||||
|
||||
4️⃣ For large files (>300 lines)
|
||||
|
||||
rtk read <file> -l aggressive
|
||||
|
||||
5️⃣ For quick understanding
|
||||
|
||||
rtk smart <file>
|
||||
|
||||
Never open many files blindly.
|
||||
|
||||
Never read entire modules without searching first.
|
||||
|
||||
---
|
||||
|
||||
# 🧠 DIFF INSPECTION RULES
|
||||
|
||||
For reviewing repository changes:
|
||||
|
||||
Use:
|
||||
|
||||
rtk git diff
|
||||
|
||||
For large diffs:
|
||||
|
||||
rtk git diff -l aggressive
|
||||
|
||||
Never run raw `git diff`.
|
||||
|
||||
---
|
||||
|
||||
# 📁 FORBIDDEN PATHS
|
||||
|
||||
Never inspect or read these directories unless explicitly required:
|
||||
|
||||
node_modules/
|
||||
dist/
|
||||
build/
|
||||
coverage/
|
||||
.prisma/
|
||||
prisma/migrations/
|
||||
|
||||
These folders produce extremely large outputs and waste tokens.
|
||||
|
||||
---
|
||||
|
||||
# 📄 FILE READING POLICY
|
||||
|
||||
Preferred:
|
||||
|
||||
rtk read file.ts
|
||||
|
||||
Large file:
|
||||
|
||||
rtk read file.ts -l aggressive
|
||||
|
||||
Quick overview:
|
||||
|
||||
rtk smart file.ts
|
||||
|
||||
Never use `cat` for source code inspection.
|
||||
|
||||
---
|
||||
|
||||
# 🔎 SEARCH POLICY
|
||||
|
||||
Always search before opening files.
|
||||
|
||||
Use:
|
||||
|
||||
rtk grep <pattern>
|
||||
|
||||
Avoid raw recursive searches.
|
||||
|
||||
---
|
||||
|
||||
# 🎯 TOKEN SAFETY RULES
|
||||
|
||||
- Never scan the entire repository.
|
||||
- Never dump full logs.
|
||||
- Never output entire large files.
|
||||
- Prefer targeted inspection.
|
||||
|
||||
Token preservation is mandatory for this project.
|
||||
|
||||
---
|
||||
|
||||
## Scope
|
||||
|
||||
- Applies to the whole repository.
|
||||
- Stack: NestJS + Prisma + TypeScript.
|
||||
|
||||
## Primary Goals
|
||||
|
||||
- Deliver minimal, safe, and focused changes.
|
||||
- Preserve existing API behavior unless explicitly requested.
|
||||
- Keep Prisma schema/data operations forward-safe.
|
||||
|
||||
## Project Conventions
|
||||
|
||||
- Keep module layering consistent: `controller -> service -> prisma/shared service`.
|
||||
- Reuse shared services for cross-module business logic (for example, sales invoice create flow).
|
||||
- Keep DTO validation at API boundaries; avoid unchecked `any` in new code.
|
||||
@@ -19,6 +164,7 @@ Operational guide for AI/coding agents working in `consumer_api`.
|
||||
- Prefer explicit Prisma `select`/`include` to control payload shape.
|
||||
|
||||
## Sales Invoice / TSP Rules
|
||||
|
||||
- `originalSend`, `correctionSend`, and `revoke` flows should be consistent and auditable.
|
||||
- For correction/revoke creation, prepare data from the related invoice when required.
|
||||
- Persist attempt records with clear status transitions (`QUEUED` -> final status).
|
||||
@@ -26,6 +172,7 @@ Operational guide for AI/coding agents working in `consumer_api`.
|
||||
- Avoid changing fiscal/tax status semantics without explicit approval.
|
||||
|
||||
## Prisma and Migration Safety
|
||||
|
||||
- Treat committed migrations as immutable history.
|
||||
- Prefer forward migrations; avoid destructive resets unless explicitly requested.
|
||||
- For data-affecting changes:
|
||||
@@ -34,12 +181,14 @@ Operational guide for AI/coding agents working in `consumer_api`.
|
||||
- keep logic idempotent where possible.
|
||||
|
||||
## Editing Principles
|
||||
|
||||
- Do not modify unrelated files.
|
||||
- Do not revert user changes unless asked.
|
||||
- Keep functions cohesive; extract shared logic when duplication appears.
|
||||
- Remove debug leftovers (`console.log`, dead code) before finishing unless explicitly needed.
|
||||
|
||||
## Validation Checklist (before handoff)
|
||||
|
||||
1. Read target module/service/DTO end-to-end before edits.
|
||||
2. Apply minimal patch with consistent naming.
|
||||
3. Run typecheck: `pnpm -s tsc --noEmit`.
|
||||
@@ -47,12 +196,14 @@ Operational guide for AI/coding agents working in `consumer_api`.
|
||||
5. Summarize changed files and behavior impact clearly.
|
||||
|
||||
## Useful Commands
|
||||
|
||||
- Typecheck: `pnpm -s tsc --noEmit`
|
||||
- Migration status: `pnpm prisma migrate status`
|
||||
- Create migration: `pnpm prisma migrate dev --name <name>`
|
||||
- Deploy migrations: `pnpm prisma migrate deploy`
|
||||
|
||||
## Communication Style
|
||||
|
||||
- Be concise and implementation-focused.
|
||||
- Call out assumptions/risk before risky steps.
|
||||
- Provide practical next actions after task completion.
|
||||
@@ -60,6 +211,7 @@ Operational guide for AI/coding agents working in `consumer_api`.
|
||||
## Do / Don't (Repo-Specific)
|
||||
|
||||
### Do
|
||||
|
||||
- Do derive correction/revoke invoice creation data from `relatedInvoice` when the flow requires historical consistency.
|
||||
- Do keep TSP attempt lifecycle explicit (`QUEUED`, then update with provider result and timestamps).
|
||||
- Do use shared invoice-creation service instead of duplicating create logic across modules.
|
||||
@@ -67,6 +219,7 @@ Operational guide for AI/coding agents working in `consumer_api`.
|
||||
- Do keep Prisma queries tight with `select`/`include` only for fields you actually use.
|
||||
|
||||
### Don't
|
||||
|
||||
- Don't pass undefined/out-of-scope variables in TSP flows (common regressions: `invoice_id`, `attemptId`, `pos_id` mismatches).
|
||||
- Don't leave placeholder query blocks (for example empty `select: {}`) in production code.
|
||||
- Don't mix method semantics (`send` vs `originalSend`) across services without verifying signatures.
|
||||
@@ -74,12 +227,14 @@ Operational guide for AI/coding agents working in `consumer_api`.
|
||||
- Don't change invoice type semantics (`ORIGINAL`, `CORRECTION`, `REVOKE`) implicitly.
|
||||
|
||||
### Common Pitfalls To Recheck
|
||||
|
||||
- Incorrect relation field names (`tax_id` on wrong model, missing relation selects).
|
||||
- Building payloads from the wrong invoice (must match the expected new/ref invoice in each flow).
|
||||
- Creating attempts without persisting request payload and final response payload.
|
||||
- Mismatch between DTO shapes and shared service input contracts.
|
||||
|
||||
## Thread Notes (May 2026)
|
||||
|
||||
- Shared sale-invoice creation was introduced and must be injected/exported correctly in consuming modules (example failure: `UnknownDependenciesException` for `SharedSaleInvoiceCreateService`).
|
||||
- In `SalesInvoiceTspService.revoke`, prepare all creation/update data from `relatedInvoice` (no external `dataToUpdate` argument expected).
|
||||
- Prisma client is generated to `src/generated/prisma` via:
|
||||
@@ -94,6 +249,7 @@ Operational guide for AI/coding agents working in `consumer_api`.
|
||||
- For heavy license provisioning (100+), request path should not synchronously insert all licenses; queue/background + batching is required.
|
||||
|
||||
## Migration Drift Playbook (Prisma/MySQL)
|
||||
|
||||
- If Prisma reports `modified after applied`, never edit an already-applied migration in-place for shared environments; create a new forward migration instead.
|
||||
- If migration history and DB drift mismatch:
|
||||
1. Verify local migration folders are complete and ordered.
|
||||
@@ -104,6 +260,7 @@ Operational guide for AI/coding agents working in `consumer_api`.
|
||||
- `prisma migrate status` may show up-to-date while `migrate dev` still detects drift (shadow DB/application history issue); treat `migrate dev` output as source of truth for fixing local history.
|
||||
|
||||
## Redis Cache Conventions (May 2026)
|
||||
|
||||
- Use `RedisKeyMaker` in `src/common/utils/redis-key-maker.util.ts` for all cache keys and wildcard patterns; do not inline key strings in services.
|
||||
- Keep invalidation domain-based (for example `src/modules/admin/guilds/cache/*`, `src/modules/admin/partners/cache/*`, `src/modules/pos/cache/*`) and avoid duplicating delete logic across modules.
|
||||
- For list APIs that are expensive or frequently read, prefer read-through cache with TTL and invalidation on every related write path.
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -57,26 +57,22 @@ export class GoodsService {
|
||||
|
||||
async findAll(guildId: string) {
|
||||
const cacheKey = RedisKeyMaker.guildGoodsList(guildId)
|
||||
const cached = await this.redisService.getJson<{ goods: unknown[]; total: number }>(
|
||||
return this.redisService.getAndSet(
|
||||
cacheKey,
|
||||
'paginate',
|
||||
async () => {
|
||||
const [goods, total] = await this.prisma.$transaction([
|
||||
this.prisma.good.findMany({
|
||||
where: this.defaultWhere(guildId),
|
||||
select: this.defaultSelect,
|
||||
}),
|
||||
this.prisma.good.count(),
|
||||
])
|
||||
|
||||
return { items: goods, total }
|
||||
},
|
||||
this.listCacheTtlSeconds,
|
||||
)
|
||||
if (cached) {
|
||||
return ResponseMapper.paginate(cached.goods, { total: cached.total })
|
||||
}
|
||||
|
||||
const [goods, total] = await this.prisma.$transaction([
|
||||
this.prisma.good.findMany({
|
||||
where: this.defaultWhere(guildId),
|
||||
select: this.defaultSelect,
|
||||
}),
|
||||
this.prisma.good.count(),
|
||||
])
|
||||
|
||||
await this.redisService.setJson(cacheKey, { goods, total }, this.listCacheTtlSeconds)
|
||||
|
||||
return ResponseMapper.paginate(goods, {
|
||||
total,
|
||||
})
|
||||
}
|
||||
|
||||
async findOne(guild_id: string, id: string) {
|
||||
|
||||
@@ -31,20 +31,20 @@ export class StockKeepingUnitsService {
|
||||
|
||||
async findAll(guild_id: string) {
|
||||
const cacheKey = this.getListCacheKey(guild_id)
|
||||
const cached = await this.redisService.getJson<unknown[]>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.list(cached)
|
||||
}
|
||||
return await this.redisService.getAndSet(
|
||||
cacheKey,
|
||||
'list',
|
||||
async () => {
|
||||
const items = await this.prisma.stockKeepingUnits.findMany({
|
||||
where: { guild_id },
|
||||
orderBy: { created_at: 'desc' },
|
||||
})
|
||||
|
||||
const items = await this.prisma.stockKeepingUnits.findMany({
|
||||
where: { guild_id },
|
||||
orderBy: { created_at: 'desc' },
|
||||
})
|
||||
|
||||
const mappedData = items.map(this.mapData)
|
||||
await this.redisService.setJson(cacheKey, mappedData, this.listCacheTtlSeconds)
|
||||
|
||||
return ResponseMapper.list(mappedData)
|
||||
const mappedData = items.map(this.mapData)
|
||||
return mappedData
|
||||
},
|
||||
this.listCacheTtlSeconds,
|
||||
)
|
||||
}
|
||||
|
||||
async create(guild_id: string, data: CreateStockKeepingUnitDto) {
|
||||
|
||||
@@ -17,14 +17,6 @@ export class PartnerActivatedLicensesService {
|
||||
) {}
|
||||
|
||||
async findAll(partner_id: string, page = 1, perPage = 10) {
|
||||
const cacheKey = RedisKeyMaker.partnerActivatedLicensesList(partner_id, page, perPage)
|
||||
const cached = await this.redisService.getJson<{ data: unknown[]; total: number }>(
|
||||
cacheKey,
|
||||
)
|
||||
if (cached) {
|
||||
return ResponseMapper.paginate(cached.data, { page, perPage, total: cached.total })
|
||||
}
|
||||
|
||||
const defaultWhere: LicenseActivationWhereInput = {
|
||||
license: {
|
||||
charge_transaction: {
|
||||
@@ -32,69 +24,70 @@ export class PartnerActivatedLicensesService {
|
||||
},
|
||||
},
|
||||
}
|
||||
const [activatedLicense, total] = await this.prisma.$transaction(async tx => [
|
||||
await tx.licenseActivation.findMany({
|
||||
where: defaultWhere,
|
||||
skip: (page - 1) * perPage,
|
||||
take: perPage,
|
||||
select: {
|
||||
id: true,
|
||||
starts_at: true,
|
||||
expires_at: true,
|
||||
created_at: true,
|
||||
account_allocations: {
|
||||
select: {
|
||||
id: true,
|
||||
account_id: true,
|
||||
const cacheKey = RedisKeyMaker.partnerActivatedLicensesList(partner_id, page, perPage)
|
||||
return await this.redisService.getAndSet(cacheKey, 'paginate', async () => {
|
||||
const [activatedLicense, total] = await this.prisma.$transaction(async tx => [
|
||||
await tx.licenseActivation.findMany({
|
||||
where: defaultWhere,
|
||||
skip: (page - 1) * perPage,
|
||||
take: perPage,
|
||||
select: {
|
||||
id: true,
|
||||
starts_at: true,
|
||||
expires_at: true,
|
||||
created_at: true,
|
||||
account_allocations: {
|
||||
select: {
|
||||
id: true,
|
||||
account_id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
business_activity: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
consumer: {
|
||||
select: {
|
||||
id: true,
|
||||
...QUERY_CONSTANTS.CONSUMER.infoSelect,
|
||||
business_activity: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
consumer: {
|
||||
select: {
|
||||
id: true,
|
||||
...QUERY_CONSTANTS.CONSUMER.infoSelect,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
await tx.licenseActivation.count({
|
||||
where: defaultWhere,
|
||||
}),
|
||||
])
|
||||
}),
|
||||
await tx.licenseActivation.count({
|
||||
where: defaultWhere,
|
||||
}),
|
||||
])
|
||||
|
||||
const mappedLicenses = activatedLicense.map(license => {
|
||||
const { account_allocations, business_activity, ...rest } = license
|
||||
const { consumer, ...businessActivityRest } = business_activity
|
||||
const mappedLicenses = activatedLicense.map(license => {
|
||||
const { account_allocations, business_activity, ...rest } = license
|
||||
const { consumer, ...businessActivityRest } = business_activity
|
||||
|
||||
const mappedConsumer = consumer_mappersUtil(consumer)
|
||||
const mappedConsumer = consumer_mappersUtil(consumer)
|
||||
|
||||
return {
|
||||
...rest,
|
||||
license: {
|
||||
accounts_limit: account_allocations.length,
|
||||
allocated_account_count: account_allocations.filter(
|
||||
allocation => allocation.account_id !== null,
|
||||
).length,
|
||||
},
|
||||
business_activity: {
|
||||
...business_activity,
|
||||
consumer_name: mappedConsumer.name,
|
||||
},
|
||||
}
|
||||
})
|
||||
return {
|
||||
...rest,
|
||||
license: {
|
||||
accounts_limit: account_allocations.length,
|
||||
allocated_account_count: account_allocations.filter(
|
||||
allocation => allocation.account_id !== null,
|
||||
).length,
|
||||
},
|
||||
business_activity: {
|
||||
...business_activity,
|
||||
consumer_name: mappedConsumer.name,
|
||||
},
|
||||
items: mappedLicenses,
|
||||
page,
|
||||
perPage,
|
||||
total,
|
||||
}
|
||||
})
|
||||
|
||||
await this.redisService.setJson(cacheKey, { data: mappedLicenses, total }, 300)
|
||||
|
||||
return ResponseMapper.paginate(mappedLicenses, {
|
||||
page,
|
||||
perPage,
|
||||
total,
|
||||
})
|
||||
}
|
||||
|
||||
// async update(partnerId: string, id: string, data: UpdateLicenseDto) {
|
||||
|
||||
+28
-39
@@ -65,56 +65,45 @@ export class PartnerLicenseChargeTransactionService {
|
||||
page,
|
||||
perPage,
|
||||
)
|
||||
const cached = await this.redisService.getJson<{ data: unknown[]; total: number }>(
|
||||
cacheKey,
|
||||
)
|
||||
if (cached) {
|
||||
return ResponseMapper.paginate(cached.data, { page, perPage, total: cached.total })
|
||||
}
|
||||
|
||||
const defaultWhere: LicenseChargeTransactionWhereInput = {
|
||||
partner_id,
|
||||
}
|
||||
return await this.redisService.getAndSet(cacheKey, 'paginate', async () => {
|
||||
const [transactions, total] = await this.prisma.$transaction(async tx => [
|
||||
await tx.licenseChargeTransaction.findMany({
|
||||
where: defaultWhere,
|
||||
skip: (page - 1) * perPage,
|
||||
take: perPage,
|
||||
select: this.defaultSelect,
|
||||
}),
|
||||
await tx.licenseChargeTransaction.count({
|
||||
where: defaultWhere,
|
||||
}),
|
||||
])
|
||||
|
||||
const [transactions, total] = await this.prisma.$transaction(async tx => [
|
||||
await tx.licenseChargeTransaction.findMany({
|
||||
where: defaultWhere,
|
||||
skip: (page - 1) * perPage,
|
||||
take: perPage,
|
||||
select: this.defaultSelect,
|
||||
}),
|
||||
await tx.licenseChargeTransaction.count({
|
||||
where: defaultWhere,
|
||||
}),
|
||||
])
|
||||
|
||||
const mappedTransactions = transactions.map(this.mappedTransaction)
|
||||
await this.redisService.setJson(cacheKey, { data: mappedTransactions, total }, 300)
|
||||
|
||||
return ResponseMapper.paginate(mappedTransactions, {
|
||||
page,
|
||||
perPage,
|
||||
total,
|
||||
const mappedTransactions = transactions.map(this.mappedTransaction)
|
||||
return {
|
||||
items: mappedTransactions,
|
||||
page,
|
||||
perPage,
|
||||
total,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async findOne(partner_id: string, id: string) {
|
||||
const cacheKey = RedisKeyMaker.partnerLicenseChargeTransactionDetail(partner_id, id)
|
||||
const cached = await this.redisService.getJson<unknown>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.single(cached)
|
||||
}
|
||||
|
||||
const transaction = await this.prisma.licenseChargeTransaction.findUniqueOrThrow({
|
||||
where: {
|
||||
id,
|
||||
partner_id,
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
return await this.redisService.getAndSet(cacheKey, 'single', async () => {
|
||||
const transaction = await this.prisma.licenseChargeTransaction.findUniqueOrThrow({
|
||||
where: {
|
||||
id,
|
||||
partner_id,
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return this.mappedTransaction(transaction)
|
||||
})
|
||||
const mappedTransaction = this.mappedTransaction(transaction)
|
||||
await this.redisService.setJson(cacheKey, mappedTransaction, 300)
|
||||
return ResponseMapper.single(mappedTransaction)
|
||||
}
|
||||
|
||||
async create(partner_id: string, data: ChargeLicenseDto) {
|
||||
|
||||
@@ -143,39 +143,26 @@ export class PartnersService {
|
||||
|
||||
async findAll() {
|
||||
const cacheKey = RedisKeyMaker.partnersList()
|
||||
const cached = await this.redisService.getJson<unknown[]>(cacheKey)
|
||||
if (cached) {
|
||||
console.log('cached', cached)
|
||||
return await this.redisService.getAndSet(cacheKey, 'list', async () => {
|
||||
const partners = await this.prisma.partner.findMany({
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
return ResponseMapper.list(cached)
|
||||
}
|
||||
|
||||
const partners = await this.prisma.partner.findMany({
|
||||
select: this.defaultSelect,
|
||||
return partners.map(this.mapPartner)
|
||||
})
|
||||
|
||||
const mappedPartners = partners.map(this.mapPartner)
|
||||
await this.redisService.setJson(cacheKey, mappedPartners, 300)
|
||||
|
||||
return ResponseMapper.list(mappedPartners)
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const cacheKey = RedisKeyMaker.partnerDetail(id)
|
||||
const cached = await this.redisService.getJson<unknown>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.single(cached)
|
||||
}
|
||||
|
||||
const partner = await this.prisma.partner.findUniqueOrThrow({
|
||||
where: { id },
|
||||
select: this.defaultSelect,
|
||||
return await this.redisService.getAndSet(cacheKey, 'single', async () => {
|
||||
const partner = await this.prisma.partner.findUniqueOrThrow({
|
||||
where: { id },
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
return this.mapPartner(partner)
|
||||
})
|
||||
|
||||
const mappedPartner = this.mapPartner(partner)
|
||||
await this.redisService.setJson(cacheKey, mappedPartner, 300)
|
||||
|
||||
return ResponseMapper.single(mappedPartner)
|
||||
}
|
||||
|
||||
async create(data: CreatePartnerDto, logo?: any) {
|
||||
|
||||
@@ -61,34 +61,23 @@ export class BusinessActivitiesService {
|
||||
|
||||
async findAll(consumer_id: string) {
|
||||
const cacheKey = RedisKeyMaker.consumerBusinessActivitiesList(consumer_id)
|
||||
const cached = await this.redisService.getJson<unknown[]>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.list(cached)
|
||||
}
|
||||
|
||||
const businessActivities =
|
||||
await this.businessActivitiesQueryService.findAllByConsumer(
|
||||
return await this.redisService.getAndSet(cacheKey, 'list', async () => {
|
||||
return await this.businessActivitiesQueryService.findAllByConsumer(
|
||||
consumer_id,
|
||||
this.defaultSelect,
|
||||
)
|
||||
await this.redisService.setJson(cacheKey, businessActivities, this.cacheTtlSeconds)
|
||||
return ResponseMapper.list(businessActivities)
|
||||
})
|
||||
}
|
||||
|
||||
async findOne(consumer_id: string, id: string) {
|
||||
const cacheKey = RedisKeyMaker.consumerBusinessActivityInfo(consumer_id, id)
|
||||
const cached = await this.redisService.getJson<unknown>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.single(cached)
|
||||
}
|
||||
|
||||
const businessActivity = await this.businessActivitiesQueryService.findOneByConsumer(
|
||||
consumer_id,
|
||||
id,
|
||||
this.defaultSelect,
|
||||
)
|
||||
await this.redisService.setJson(cacheKey, businessActivity, this.cacheTtlSeconds)
|
||||
return ResponseMapper.single(businessActivity)
|
||||
return await this.redisService.getAndSet(cacheKey, 'list', async () => {
|
||||
return await this.businessActivitiesQueryService.findOneByConsumer(
|
||||
consumer_id,
|
||||
id,
|
||||
this.defaultSelect,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
async update(consumer_id: string, id: string, data: any) {
|
||||
|
||||
@@ -21,25 +21,21 @@ export class ConsumerService {
|
||||
|
||||
async getInfo(consumer_id: string) {
|
||||
const cacheKey = RedisKeyMaker.consumerInfo(consumer_id)
|
||||
const cached = await this.redisService.getJson<unknown>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.single(cached)
|
||||
}
|
||||
|
||||
const consumer = await this.prisma.consumer.findUniqueOrThrow({
|
||||
where: {
|
||||
id: consumer_id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
status: true,
|
||||
...QUERY_CONSTANTS.CONSUMER.infoSelect,
|
||||
},
|
||||
return await this.redisService.getAndSet(cacheKey, 'single', async () => {
|
||||
const consumer = await this.prisma.consumer.findUniqueOrThrow({
|
||||
where: {
|
||||
id: consumer_id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
status: true,
|
||||
...QUERY_CONSTANTS.CONSUMER.infoSelect,
|
||||
},
|
||||
})
|
||||
|
||||
return consumer_mappersUtil(consumer)
|
||||
})
|
||||
|
||||
const mappedConsumer = consumer_mappersUtil(consumer)
|
||||
await this.redisService.setJson(cacheKey, mappedConsumer, this.infoCacheTtlSeconds)
|
||||
return ResponseMapper.single(mappedConsumer)
|
||||
}
|
||||
|
||||
async updateInfo(consumer_id: string, data: UpdateConsumerInfoDto) {
|
||||
|
||||
@@ -103,30 +103,26 @@ export class BusinessActivitiesService {
|
||||
page,
|
||||
perPage,
|
||||
)
|
||||
const cached = await this.redisService.getJson<{ items: unknown[]; total: number }>(
|
||||
cacheKey,
|
||||
)
|
||||
if (cached) {
|
||||
return ResponseMapper.paginate(cached.items, { total: cached.total, page, perPage })
|
||||
}
|
||||
return await this.redisService.getAndSet(cacheKey, 'paginate', async () => {
|
||||
const where = this.defaultWhere(partner_id, consumer_id)
|
||||
const [businessActivities, total] = await this.prisma.$transaction(async tx => [
|
||||
await tx.businessActivity.findMany({
|
||||
where,
|
||||
select: this.defaultSelect,
|
||||
skip: (page - 1) * perPage,
|
||||
take: perPage,
|
||||
}),
|
||||
await tx.businessActivity.count({ where }),
|
||||
])
|
||||
const mappedItems = businessActivities.map(this.prepareBusinessActivityResponse)
|
||||
|
||||
const where = this.defaultWhere(partner_id, consumer_id)
|
||||
const [businessActivities, total] = await this.prisma.$transaction(async tx => [
|
||||
await tx.businessActivity.findMany({
|
||||
where,
|
||||
select: this.defaultSelect,
|
||||
skip: (page - 1) * perPage,
|
||||
take: perPage,
|
||||
}),
|
||||
await tx.businessActivity.count({ where }),
|
||||
])
|
||||
const mappedItems = businessActivities.map(this.prepareBusinessActivityResponse)
|
||||
await this.redisService.setJson(
|
||||
cacheKey,
|
||||
{ items: mappedItems, total },
|
||||
this.cacheTtlSeconds,
|
||||
)
|
||||
return ResponseMapper.paginate(mappedItems, { total, page, perPage })
|
||||
return {
|
||||
items: mappedItems,
|
||||
total,
|
||||
page,
|
||||
perPage,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async findOne(partner_id: string, consumer_id: string, id: string) {
|
||||
@@ -135,21 +131,16 @@ export class BusinessActivitiesService {
|
||||
consumer_id,
|
||||
id,
|
||||
)
|
||||
const cached = await this.redisService.getJson<unknown>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.single(cached)
|
||||
}
|
||||
|
||||
const businessActivity = await this.prisma.businessActivity.findUnique({
|
||||
where: {
|
||||
...this.defaultWhere(partner_id, consumer_id),
|
||||
id,
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
return await this.redisService.getAndSet(cacheKey, 'single', async () => {
|
||||
const businessActivity = await this.prisma.businessActivity.findUnique({
|
||||
where: {
|
||||
...this.defaultWhere(partner_id, consumer_id),
|
||||
id,
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
return this.prepareBusinessActivityResponse(businessActivity)
|
||||
})
|
||||
const mappedItem = this.prepareBusinessActivityResponse(businessActivity)
|
||||
await this.redisService.setJson(cacheKey, mappedItem, this.cacheTtlSeconds)
|
||||
return ResponseMapper.single(mappedItem)
|
||||
}
|
||||
|
||||
async create(
|
||||
|
||||
@@ -65,44 +65,40 @@ export class BusinessActivityComplexesService {
|
||||
page,
|
||||
perPage,
|
||||
)
|
||||
const cached = await this.redisService.getJson<{ items: unknown[]; total: number }>(
|
||||
cacheKey,
|
||||
)
|
||||
if (cached) {
|
||||
return ResponseMapper.paginate(cached.items, { total: cached.total, page, perPage })
|
||||
}
|
||||
|
||||
const where = this.defaultWhere(partner_id, consumer_id, business_activity_id)
|
||||
const [complexes, total] = await this.prisma.$transaction(async tx => [
|
||||
await tx.complex.findMany({
|
||||
where,
|
||||
select: {
|
||||
...this.defaultSelect,
|
||||
_count: {
|
||||
select: {
|
||||
pos_list: true,
|
||||
return await this.redisService.getAndSet(cacheKey, 'paginate', async () => {
|
||||
const where = this.defaultWhere(partner_id, consumer_id, business_activity_id)
|
||||
const [complexes, total] = await this.prisma.$transaction(async tx => [
|
||||
await tx.complex.findMany({
|
||||
where,
|
||||
select: {
|
||||
...this.defaultSelect,
|
||||
_count: {
|
||||
select: {
|
||||
pos_list: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
skip: (page - 1) * perPage,
|
||||
take: perPage,
|
||||
}),
|
||||
await tx.complex.count({ where }),
|
||||
])
|
||||
skip: (page - 1) * perPage,
|
||||
take: perPage,
|
||||
}),
|
||||
await tx.complex.count({ where }),
|
||||
])
|
||||
|
||||
const mappedComplexes = complexes.map(complex => {
|
||||
const { _count, ...rest } = complex
|
||||
return {
|
||||
...rest,
|
||||
pos_count: _count.pos_list,
|
||||
}
|
||||
})
|
||||
|
||||
const mappedComplexes = complexes.map(complex => {
|
||||
const { _count, ...rest } = complex
|
||||
return {
|
||||
...rest,
|
||||
pos_count: _count.pos_list,
|
||||
items: mappedComplexes,
|
||||
total,
|
||||
page,
|
||||
perPage,
|
||||
}
|
||||
})
|
||||
await this.redisService.setJson(
|
||||
cacheKey,
|
||||
{ items: mappedComplexes, total },
|
||||
this.cacheTtlSeconds,
|
||||
)
|
||||
return ResponseMapper.paginate(mappedComplexes, { total, page, perPage })
|
||||
}
|
||||
|
||||
async findOne(
|
||||
@@ -117,25 +113,21 @@ export class BusinessActivityComplexesService {
|
||||
business_activity_id,
|
||||
id,
|
||||
)
|
||||
const cached = await this.redisService.getJson<unknown>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.single(cached)
|
||||
}
|
||||
return await this.redisService.getAndSet(cacheKey, 'single', async () => {
|
||||
const complex = await this.prisma.complex.findFirst({
|
||||
where: {
|
||||
...this.defaultWhere(partner_id, consumer_id, business_activity_id),
|
||||
id,
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
const complex = await this.prisma.complex.findFirst({
|
||||
where: {
|
||||
...this.defaultWhere(partner_id, consumer_id, business_activity_id),
|
||||
id,
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
if (!complex) {
|
||||
throw new BadRequestException('شعبه مورد نظر یافت نشد.')
|
||||
}
|
||||
|
||||
return complex
|
||||
})
|
||||
|
||||
if (!complex) {
|
||||
throw new BadRequestException('شعبه مورد نظر یافت نشد.')
|
||||
}
|
||||
|
||||
await this.redisService.setJson(cacheKey, complex, this.cacheTtlSeconds)
|
||||
return ResponseMapper.single(complex)
|
||||
}
|
||||
|
||||
async create(
|
||||
|
||||
@@ -64,60 +64,42 @@ export class PartnerConsumersService {
|
||||
|
||||
async findAll(partner_id: string, page = 1, perPage = 10) {
|
||||
const cacheKey = RedisKeyMaker.partnerConsumersList(partner_id, page, perPage)
|
||||
const cached = await this.redisService.getJson<{ items: unknown[]; total: number }>(
|
||||
cacheKey,
|
||||
)
|
||||
if (cached) {
|
||||
return ResponseMapper.paginate(cached.items, { total: cached.total, page, perPage })
|
||||
}
|
||||
return await this.redisService.getAndSet(cacheKey, 'paginate', async () => {
|
||||
const [consumers, total] = await this.prisma.$transaction(async tx => [
|
||||
await tx.consumer.findMany({
|
||||
where: this.defaultWhere(partner_id),
|
||||
select: this.defaultSelect,
|
||||
skip: (page - 1) * perPage,
|
||||
take: 10,
|
||||
}),
|
||||
await tx.consumer.count({
|
||||
where: this.defaultWhere(partner_id),
|
||||
}),
|
||||
])
|
||||
const mappedConsumers = consumers.map(mapConsumerWithLicenseUtil)
|
||||
|
||||
const [consumers, total] = await this.prisma.$transaction(async tx => [
|
||||
await tx.consumer.findMany({
|
||||
where: this.defaultWhere(partner_id),
|
||||
select: this.defaultSelect,
|
||||
skip: (page - 1) * perPage,
|
||||
take: 10,
|
||||
}),
|
||||
await tx.consumer.count({
|
||||
where: this.defaultWhere(partner_id),
|
||||
}),
|
||||
])
|
||||
const mappedItems = consumers.map(mapConsumerWithLicenseUtil)
|
||||
await this.redisService.setJson(
|
||||
cacheKey,
|
||||
{ items: mappedItems, total },
|
||||
this.infoCacheTtlSeconds,
|
||||
)
|
||||
return ResponseMapper.paginate(mappedItems, {
|
||||
total,
|
||||
page,
|
||||
perPage,
|
||||
return {
|
||||
items: mappedConsumers,
|
||||
total,
|
||||
page,
|
||||
perPage,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async findOne(partner_id: string, consumer_id: string) {
|
||||
const cacheKey = RedisKeyMaker.partnerConsumerInfo(partner_id, consumer_id)
|
||||
const cached = await this.redisService.getJson<unknown>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.single(cached)
|
||||
}
|
||||
return await this.redisService.getAndSet(cacheKey, 'single', async () => {
|
||||
const consumer = await this.prisma.consumer.findUniqueOrThrow({
|
||||
where: {
|
||||
...(this.defaultWhere(partner_id) as any),
|
||||
id: consumer_id,
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
const consumer = await this.prisma.consumer.findUniqueOrThrow({
|
||||
where: {
|
||||
...(this.defaultWhere(partner_id) as any),
|
||||
id: consumer_id,
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
return mapConsumerWithLicenseUtil(consumer)
|
||||
})
|
||||
|
||||
const mappedConsumer = mapConsumerWithLicenseUtil(consumer)
|
||||
await this.redisService.setJson(
|
||||
RedisKeyMaker.consumerInfo(consumer_id),
|
||||
mappedConsumer,
|
||||
this.infoCacheTtlSeconds,
|
||||
)
|
||||
await this.redisService.setJson(cacheKey, mappedConsumer, this.infoCacheTtlSeconds)
|
||||
return ResponseMapper.single(mappedConsumer)
|
||||
}
|
||||
|
||||
async create(partner_id: string, data: CreateConsumerDto) {
|
||||
|
||||
@@ -52,47 +52,44 @@ export class GoodsService {
|
||||
guild_id: string,
|
||||
consumer_account_id: string,
|
||||
) {
|
||||
const cacheKey = RedisKeyMaker.posGoodsList(guild_id, business_activity_id)
|
||||
try {
|
||||
const cached = await this.redisService.getJson<unknown[]>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.list(cached)
|
||||
}
|
||||
throw new Error('Cache miss')
|
||||
} catch (error) {
|
||||
const goods = await this.prisma.good.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
{
|
||||
is_default_guild_good: true,
|
||||
category: {
|
||||
guild_id,
|
||||
const cacheKey = RedisKeyMaker.posGoodsList(business_activity_id, guild_id)
|
||||
return this.redisService.getAndSet(
|
||||
cacheKey,
|
||||
'list',
|
||||
async () => {
|
||||
const goods = await this.prisma.good.findMany({
|
||||
where: {
|
||||
OR: [
|
||||
{
|
||||
is_default_guild_good: true,
|
||||
category: {
|
||||
guild_id,
|
||||
},
|
||||
},
|
||||
{
|
||||
business_activity_id,
|
||||
},
|
||||
],
|
||||
},
|
||||
select: {
|
||||
...this.defaultSelect,
|
||||
consumer_account_good_favorites: {
|
||||
where: {
|
||||
consumer_account_id,
|
||||
},
|
||||
},
|
||||
{
|
||||
business_activity_id,
|
||||
},
|
||||
],
|
||||
},
|
||||
select: {
|
||||
...this.defaultSelect,
|
||||
consumer_account_good_favorites: {
|
||||
where: {
|
||||
consumer_account_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
})
|
||||
|
||||
const mappedGoods = goods.map(good => ({
|
||||
...good,
|
||||
is_favorite: good.consumer_account_good_favorites.length > 0,
|
||||
}))
|
||||
const mappedGoods = goods.map(good => ({
|
||||
...good,
|
||||
is_favorite: good.consumer_account_good_favorites.length > 0,
|
||||
}))
|
||||
|
||||
await this.redisService.setJson(cacheKey, mappedGoods, this.listCacheTtlSeconds)
|
||||
|
||||
return ResponseMapper.list(mappedGoods)
|
||||
}
|
||||
return mappedGoods
|
||||
},
|
||||
this.listCacheTtlSeconds,
|
||||
)
|
||||
}
|
||||
|
||||
async findOne(good_id: string, business_activity_id: string, guild_id: string) {
|
||||
|
||||
+109
-105
@@ -19,93 +19,95 @@ export class PosService {
|
||||
|
||||
async getInfo(pos_id: string) {
|
||||
const cacheKey = RedisKeyMaker.posInfo(pos_id)
|
||||
const cached = await this.redisService.getJson<unknown>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.single(cached)
|
||||
}
|
||||
|
||||
const pos = await this.prisma.pos.findUniqueOrThrow({
|
||||
where: {
|
||||
id: pos_id,
|
||||
},
|
||||
select: {
|
||||
name: true,
|
||||
complex: {
|
||||
return await this.redisService.getAndSet(
|
||||
cacheKey,
|
||||
'single',
|
||||
async () => {
|
||||
const pos = await this.prisma.pos.findUniqueOrThrow({
|
||||
where: {
|
||||
id: pos_id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
branch_code: true,
|
||||
business_activity: {
|
||||
complex: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
economic_code: true,
|
||||
license_activation: {
|
||||
branch_code: true,
|
||||
business_activity: {
|
||||
select: {
|
||||
expires_at: true,
|
||||
license: {
|
||||
id: true,
|
||||
name: true,
|
||||
economic_code: true,
|
||||
license_activation: {
|
||||
select: {
|
||||
id: true,
|
||||
charge_transaction: {
|
||||
expires_at: true,
|
||||
license: {
|
||||
select: {
|
||||
partner: {
|
||||
id: true,
|
||||
charge_transaction: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
code: true,
|
||||
logo_url: true,
|
||||
partner: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
code: true,
|
||||
logo_url: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
guild: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
code: true,
|
||||
guild: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
code: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
const { name, complex } = pos
|
||||
const { name: complexName, id: complexId, business_activity } = complex
|
||||
const {
|
||||
name: businessName,
|
||||
id: businessId,
|
||||
guild,
|
||||
economic_code,
|
||||
license_activation,
|
||||
} = business_activity
|
||||
})
|
||||
const { name, complex } = pos
|
||||
const { name: complexName, id: complexId, business_activity } = complex
|
||||
const {
|
||||
name: businessName,
|
||||
id: businessId,
|
||||
guild,
|
||||
economic_code,
|
||||
license_activation,
|
||||
} = business_activity
|
||||
|
||||
const payload = {
|
||||
name,
|
||||
complex: {
|
||||
id: complexId,
|
||||
name: complexName,
|
||||
branch_code: complex.branch_code,
|
||||
const payload = {
|
||||
name,
|
||||
complex: {
|
||||
id: complexId,
|
||||
name: complexName,
|
||||
branch_code: complex.branch_code,
|
||||
},
|
||||
businessActivity: {
|
||||
id: businessId,
|
||||
name: businessName,
|
||||
economic_code,
|
||||
},
|
||||
guild,
|
||||
license_info: {
|
||||
expires_at: license_activation?.expires_at,
|
||||
license_id: license_activation?.license.id,
|
||||
},
|
||||
partner: license_activation?.license.charge_transaction.partner,
|
||||
}
|
||||
|
||||
return payload
|
||||
},
|
||||
businessActivity: {
|
||||
id: businessId,
|
||||
name: businessName,
|
||||
economic_code,
|
||||
},
|
||||
guild,
|
||||
license_info: {
|
||||
expires_at: license_activation?.expires_at,
|
||||
license_id: license_activation?.license.id,
|
||||
},
|
||||
partner: license_activation?.license.charge_transaction.partner,
|
||||
}
|
||||
await this.redisService.setJson(cacheKey, payload, this.infoCacheTtlSeconds)
|
||||
return ResponseMapper.single(payload)
|
||||
this.infoCacheTtlSeconds,
|
||||
)
|
||||
}
|
||||
|
||||
async getAccessible(account_id: string) {
|
||||
@@ -153,57 +155,59 @@ export class PosService {
|
||||
|
||||
async getMe(account_id: string, pos_id: string) {
|
||||
const cacheKey = RedisKeyMaker.posMe(account_id, pos_id)
|
||||
const cached = await this.redisService.getJson<unknown>(cacheKey)
|
||||
if (cached) {
|
||||
return ResponseMapper.single(cached)
|
||||
}
|
||||
|
||||
const pos = await this.prisma.consumerAccount.findUniqueOrThrow({
|
||||
where: {
|
||||
id: account_id,
|
||||
consumer: {
|
||||
status: ConsumerStatus.ACTIVE,
|
||||
business_activities: {
|
||||
some: {
|
||||
complexes: {
|
||||
return await this.redisService.getAndSet(
|
||||
cacheKey,
|
||||
'single',
|
||||
async () => {
|
||||
const pos = await this.prisma.consumerAccount.findUniqueOrThrow({
|
||||
where: {
|
||||
id: account_id,
|
||||
consumer: {
|
||||
status: ConsumerStatus.ACTIVE,
|
||||
business_activities: {
|
||||
some: {
|
||||
pos_list: {
|
||||
complexes: {
|
||||
some: {
|
||||
id: pos_id,
|
||||
pos_list: {
|
||||
some: {
|
||||
id: pos_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
role: true,
|
||||
consumer: {
|
||||
select: {
|
||||
id: true,
|
||||
type: true,
|
||||
status: true,
|
||||
...QUERY_CONSTANTS.CONSUMER.infoSelect,
|
||||
role: true,
|
||||
consumer: {
|
||||
select: {
|
||||
id: true,
|
||||
type: true,
|
||||
status: true,
|
||||
...QUERY_CONSTANTS.CONSUMER.infoSelect,
|
||||
},
|
||||
},
|
||||
account: {
|
||||
select: {
|
||||
username: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
account: {
|
||||
select: {
|
||||
username: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
const { consumer, ...rest } = pos
|
||||
|
||||
const payload = {
|
||||
...rest,
|
||||
consumer: consumer_mappersUtil(consumer),
|
||||
}
|
||||
|
||||
return payload
|
||||
},
|
||||
})
|
||||
|
||||
const { consumer, ...rest } = pos
|
||||
|
||||
const payload = {
|
||||
...rest,
|
||||
consumer: consumer_mappersUtil(consumer),
|
||||
}
|
||||
await this.redisService.setJson(cacheKey, payload, this.meCacheTtlSeconds)
|
||||
return ResponseMapper.single(payload)
|
||||
this.meCacheTtlSeconds,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ export class PrismaService extends PrismaClient implements OnModuleInit, OnModul
|
||||
password: env('DATABASE_PASSWORD'),
|
||||
database: env('DATABASE_NAME'),
|
||||
port: env('DATABASE_PORT') ? Number(env('DATABASE_PORT')) : 3306,
|
||||
connectionLimit: 50,
|
||||
connectionLimit: 10,
|
||||
})
|
||||
|
||||
const prismaOptions = getPrismaOptions()
|
||||
|
||||
+148
-24
@@ -1,27 +1,35 @@
|
||||
import {
|
||||
MapperWrapper,
|
||||
Paginated,
|
||||
ResponseMapper,
|
||||
} from '@/common/response/response-mapper'
|
||||
import { Injectable, Logger, OnModuleDestroy } from '@nestjs/common'
|
||||
import Redis from 'ioredis'
|
||||
|
||||
interface PaginatedCache<T> {
|
||||
items: T[]
|
||||
total: number
|
||||
page?: number
|
||||
perPage?: number
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class RedisService implements OnModuleDestroy {
|
||||
private static readonly scanCount = 100
|
||||
private readonly logger = new Logger(RedisService.name)
|
||||
private readonly client: Redis
|
||||
|
||||
constructor() {
|
||||
const host = process.env.REDIS_HOST || 'redis'
|
||||
const port = Number(process.env.REDIS_PORT || 6379)
|
||||
const password = process.env.REDIS_PASSWORD || undefined
|
||||
const db = Number(process.env.REDIS_DB || 0)
|
||||
|
||||
this.client = new Redis({
|
||||
host,
|
||||
port,
|
||||
password,
|
||||
db,
|
||||
host: process.env.REDIS_HOST || 'redis',
|
||||
port: Number(process.env.REDIS_PORT || 6379),
|
||||
password: process.env.REDIS_PASSWORD || undefined,
|
||||
db: Number(process.env.REDIS_DB || 0),
|
||||
lazyConnect: true,
|
||||
maxRetriesPerRequest: 3,
|
||||
})
|
||||
|
||||
this.client.on('error', (error) => {
|
||||
this.client.on('error', error => {
|
||||
this.logger.error(`Redis error: ${error.message}`)
|
||||
})
|
||||
}
|
||||
@@ -53,11 +61,7 @@ export class RedisService implements OnModuleDestroy {
|
||||
}
|
||||
}
|
||||
|
||||
async set(
|
||||
key: string,
|
||||
value: string,
|
||||
ttlSeconds?: number,
|
||||
): Promise<'OK' | null> {
|
||||
async set(key: string, value: string, ttlSeconds?: number): Promise<'OK' | null> {
|
||||
const client = await this.getClient()
|
||||
if (ttlSeconds && ttlSeconds > 0) {
|
||||
return client.set(key, value, 'EX', ttlSeconds)
|
||||
@@ -65,11 +69,7 @@ export class RedisService implements OnModuleDestroy {
|
||||
return client.set(key, value)
|
||||
}
|
||||
|
||||
async setJson(
|
||||
key: string,
|
||||
value: unknown,
|
||||
ttlSeconds?: number,
|
||||
): Promise<'OK' | null> {
|
||||
async setJson(key: string, value: unknown, ttlSeconds?: number): Promise<'OK' | null> {
|
||||
return this.set(key, JSON.stringify(value), ttlSeconds)
|
||||
}
|
||||
|
||||
@@ -101,14 +101,12 @@ export class RedisService implements OnModuleDestroy {
|
||||
'MATCH',
|
||||
pattern,
|
||||
'COUNT',
|
||||
100,
|
||||
RedisService.scanCount,
|
||||
)
|
||||
cursor = nextCursor
|
||||
|
||||
if (keys.length > 0) {
|
||||
const pipeline = client.pipeline()
|
||||
keys.forEach(k => pipeline.del(k))
|
||||
const results = await pipeline.exec()
|
||||
const results = await this.deleteKeys(client, keys)
|
||||
if (results) {
|
||||
deletedCount += results.reduce((sum, [, value]) => {
|
||||
return sum + (typeof value === 'number' ? value : 0)
|
||||
@@ -127,6 +125,132 @@ export class RedisService implements OnModuleDestroy {
|
||||
return deletedCounts.reduce((sum, count) => sum + count, 0)
|
||||
}
|
||||
|
||||
async getAndSet<T>(
|
||||
key: string,
|
||||
type: 'single',
|
||||
callback: () => Promise<T>,
|
||||
ttlSeconds?: number,
|
||||
): Promise<MapperWrapper<T>>
|
||||
async getAndSet<T>(
|
||||
key: string,
|
||||
type: 'list',
|
||||
callback: () => Promise<T[]>,
|
||||
ttlSeconds?: number,
|
||||
): Promise<MapperWrapper<T>>
|
||||
async getAndSet<T>(
|
||||
key: string,
|
||||
type: 'paginate',
|
||||
callback: () => Promise<PaginatedCache<T>>,
|
||||
ttlSeconds?: number,
|
||||
): Promise<Paginated<T>>
|
||||
async getAndSet<T>(
|
||||
key: string,
|
||||
type: 'single' | 'list' | 'paginate',
|
||||
callback: () => Promise<T | T[] | PaginatedCache<T>>,
|
||||
ttlSeconds?: number,
|
||||
): Promise<MapperWrapper<T> | Paginated<T>> {
|
||||
switch (type) {
|
||||
case 'single':
|
||||
return this.getAndSetSingle(key, callback as () => Promise<T>, ttlSeconds)
|
||||
case 'list':
|
||||
return this.getAndSetList(key, callback as () => Promise<T[]>, ttlSeconds)
|
||||
case 'paginate':
|
||||
return this.getAndSetPaginate(
|
||||
key,
|
||||
callback as () => Promise<PaginatedCache<T>>,
|
||||
ttlSeconds,
|
||||
)
|
||||
default: {
|
||||
const neverType: never = type
|
||||
throw new Error(`Unsupported cache type: ${neverType}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async getAndSetSingle<T>(
|
||||
key: string,
|
||||
callback: () => Promise<T>,
|
||||
ttlSeconds?: number,
|
||||
): Promise<MapperWrapper<T>> {
|
||||
const cached = await this.tryReadJson<T>(key)
|
||||
if (cached !== null) {
|
||||
return ResponseMapper.single(cached)
|
||||
}
|
||||
|
||||
const data = await callback()
|
||||
await this.tryWriteJson(key, data, ttlSeconds)
|
||||
return ResponseMapper.single(data)
|
||||
}
|
||||
|
||||
private async getAndSetList<T>(
|
||||
key: string,
|
||||
callback: () => Promise<T[]>,
|
||||
ttlSeconds?: number,
|
||||
): Promise<MapperWrapper<T>> {
|
||||
const cached = await this.tryReadJson<T[]>(key)
|
||||
if (cached !== null) {
|
||||
return ResponseMapper.list(cached)
|
||||
}
|
||||
|
||||
const data = await callback()
|
||||
await this.tryWriteJson(key, data, ttlSeconds)
|
||||
return ResponseMapper.list(data)
|
||||
}
|
||||
|
||||
private async getAndSetPaginate<T>(
|
||||
key: string,
|
||||
callback: () => Promise<PaginatedCache<T>>,
|
||||
ttlSeconds?: number,
|
||||
): Promise<Paginated<T>> {
|
||||
const cached = await this.tryReadJson<PaginatedCache<T>>(key)
|
||||
if (cached !== null) {
|
||||
return ResponseMapper.paginate(cached.items, {
|
||||
total: cached.total,
|
||||
page: cached.page,
|
||||
perPage: cached.perPage,
|
||||
})
|
||||
}
|
||||
|
||||
const data = await callback()
|
||||
await this.tryWriteJson(key, data, ttlSeconds)
|
||||
return ResponseMapper.paginate(data.items, {
|
||||
total: data.total,
|
||||
page: data.page,
|
||||
perPage: data.perPage,
|
||||
})
|
||||
}
|
||||
|
||||
private async tryReadJson<T>(key: string): Promise<T | null> {
|
||||
try {
|
||||
return await this.getJson<T>(key)
|
||||
} catch (error) {
|
||||
this.logger.warn(`Redis read failed for "${key}": ${(error as Error).message}`)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
private async tryWriteJson(
|
||||
key: string,
|
||||
value: unknown,
|
||||
ttlSeconds: number = 300,
|
||||
): Promise<void> {
|
||||
try {
|
||||
await this.setJson(key, value, ttlSeconds)
|
||||
} catch (error) {
|
||||
this.logger.warn(`Redis write failed for "${key}": ${(error as Error).message}`)
|
||||
}
|
||||
}
|
||||
|
||||
private async deleteKeys(
|
||||
client: Redis,
|
||||
keys: string[],
|
||||
): Promise<Array<[Error | null, unknown]>> {
|
||||
const pipeline = client.pipeline()
|
||||
keys.forEach(key => pipeline.del(key))
|
||||
const results = await pipeline.exec()
|
||||
return results ?? []
|
||||
}
|
||||
|
||||
async onModuleDestroy() {
|
||||
await this.client.quit()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user