-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
188 additions
and
0 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
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
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
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 @@ | ||
export { renameCycle } from './renameCycle' |
43 changes: 43 additions & 0 deletions
43
src/server/controller/assessment/renameCycle/renameCycle.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,43 @@ | ||
import { ActivityLogMessage, Assessment, Cycle } from 'meta/assessment' | ||
import { User } from 'meta/user' | ||
|
||
import { generateMetaCache } from 'server/controller/assessment/generateMetaCache' | ||
import { getOneWithCycle } from 'server/controller/assessment/getOne' | ||
import { renameDataCache } from 'server/controller/assessment/renameCycle/renameDataCache' | ||
import { renameMetadataCache } from 'server/controller/assessment/renameCycle/renameMetadataCache' | ||
import { BaseProtocol, DB } from 'server/db' | ||
import { CycleRepository } from 'server/repository/assessmentCycle/cycle' | ||
import { ActivityLogRepository } from 'server/repository/public/activityLog' | ||
|
||
type Props = { | ||
assessment: Assessment | ||
cycle: Cycle | ||
name: string | ||
user: User | ||
} | ||
|
||
type Returned = { | ||
assessment: Assessment | ||
cycle: Cycle | ||
} | ||
|
||
export const renameCycle = async (props: Props, client: BaseProtocol = DB): Promise<Returned> => { | ||
const { assessment, cycle: cycleSource, name, user } = props | ||
const { name: assessmentName } = assessment.props | ||
const { uuid: cycleUuid } = cycleSource | ||
|
||
return client.tx(async (t) => { | ||
const cycleTarget = await CycleRepository.rename({ assessment, cycle: cycleSource, name }, t) | ||
|
||
// update cache | ||
await generateMetaCache(t) | ||
await renameMetadataCache({ assessment, cycleSource, cycleTarget }, t) | ||
await renameDataCache({ assessment, cycleSource, cycleTarget }, t) | ||
|
||
const message = ActivityLogMessage.assessmentCycleRename | ||
const activityLog = { target: cycleTarget, section: 'assessment', message, user } | ||
await ActivityLogRepository.insertActivityLog({ activityLog, assessment }, t) | ||
|
||
return getOneWithCycle({ assessmentName, cycleUuid }, t) | ||
}) | ||
} |
24 changes: 24 additions & 0 deletions
24
src/server/controller/assessment/renameCycle/renameDataCache.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,24 @@ | ||
import { Assessment, Cycle } from 'meta/assessment' | ||
|
||
import { BaseProtocol, DB } from 'server/db' | ||
import { CountryRepository } from 'server/repository/assessmentCycle/country' | ||
import { DataRedisRepository } from 'server/repository/redis/data' | ||
import { Logger } from 'server/utils/logger' | ||
|
||
type Props = { | ||
assessment: Assessment | ||
cycleSource: Cycle | ||
cycleTarget: Cycle | ||
} | ||
|
||
export const renameDataCache = async (props: Props, client: BaseProtocol = DB): Promise<void> => { | ||
const { assessment, cycleSource, cycleTarget } = props | ||
const { name: assessmentName } = assessment.props | ||
const { name: cycleName } = cycleTarget | ||
|
||
const countries = await CountryRepository.getMany({ assessment, cycle: cycleTarget }, client) | ||
const countryISOs = countries.map((c) => c.countryIso) | ||
|
||
await DataRedisRepository.renameCountriesData({ assessment, countryISOs, cycleSource, cycleTarget }) | ||
Logger.info(`${assessmentName}-${cycleName}: Data renamed from redis`) | ||
} |
24 changes: 24 additions & 0 deletions
24
src/server/controller/assessment/renameCycle/renameMetadataCache.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,24 @@ | ||
import { Assessment, Cycle } from 'meta/assessment' | ||
|
||
import { BaseProtocol, DB } from 'server/db' | ||
import { RowRedisRepository } from 'server/repository/redis/row' | ||
import { SectionRedisRepository } from 'server/repository/redis/section' | ||
import { Logger } from 'server/utils/logger' | ||
|
||
type Props = { | ||
assessment: Assessment | ||
cycleSource: Cycle | ||
cycleTarget: Cycle | ||
} | ||
|
||
export const renameMetadataCache = async (props: Props, client: BaseProtocol = DB) => { | ||
const { assessment, cycleSource, cycleTarget } = props | ||
const { name: assessmentName } = assessment.props | ||
const { name: cycleName } = cycleTarget | ||
|
||
const rows = await RowRedisRepository.getRows({ assessment, force: true }, client) | ||
Logger.debug(`${assessmentName}: "${Object.keys(rows).length} rows" generated`) | ||
|
||
await SectionRedisRepository.renameCycleEntries({ assessment, cycleSource, cycleTarget }) | ||
Logger.debug(`${assessmentName}-${cycleName}: Metadata renamed from redis`) | ||
} |
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,11 +1,13 @@ | ||
import { create } from './create' | ||
import { remove } from './remove' | ||
import { removeSchema } from './removeSchema' | ||
import { rename } from './rename' | ||
import { update } from './update' | ||
|
||
export const CycleRepository = { | ||
create, | ||
remove, | ||
removeSchema, | ||
rename, | ||
update, | ||
} |
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,30 @@ | ||
import { Objects } from 'utils/objects' | ||
|
||
import { Assessment, Cycle } from 'meta/assessment' | ||
|
||
import { BaseProtocol, DB, Schemas } from 'server/db' | ||
|
||
type Props = { | ||
assessment: Assessment | ||
cycle: Cycle | ||
name: string | ||
} | ||
|
||
export const rename = async (props: Props, client: BaseProtocol = DB): Promise<Cycle> => { | ||
const { assessment, cycle, name } = props | ||
|
||
const schemaCycleSource = Schemas.getNameCycle(assessment, cycle) | ||
const schemaCycleTarget = Schemas.getNameCycle(assessment, { ...cycle, name }) | ||
|
||
await DB.query<void>(`alter schema ${schemaCycleSource} rename to ${schemaCycleTarget};`) | ||
|
||
return client.one<Cycle>( | ||
` | ||
update public.assessment_cycle | ||
set name = $2 | ||
where uuid = $1 | ||
returning *`, | ||
[cycle.uuid, name], | ||
Objects.camelize | ||
) | ||
} |
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
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,26 @@ | ||
import { CountryIso } from 'meta/area' | ||
import { Assessment, Cycle } from 'meta/assessment' | ||
|
||
import { getKeyCountry, Keys } from 'server/repository/redis/keys' | ||
import { RedisData } from 'server/repository/redis/redisData' | ||
|
||
type PropsCache = { | ||
assessment: Assessment | ||
countryISOs: Array<CountryIso> | ||
cycleSource: Cycle | ||
cycleTarget: Cycle | ||
} | ||
|
||
export const renameCountriesData = async (props: PropsCache): Promise<void> => { | ||
const { assessment, cycleSource, cycleTarget, countryISOs } = props | ||
|
||
const redis = RedisData.getInstance() | ||
|
||
await Promise.all( | ||
countryISOs.map(async (countryIso) => { | ||
const key = getKeyCountry({ assessment, cycle: cycleSource, countryIso, key: Keys.Data.data }) | ||
const keyNew = getKeyCountry({ assessment, cycle: cycleTarget, countryIso, key: Keys.Data.data }) | ||
await redis.rename(key, keyNew) | ||
}) | ||
) | ||
} |
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
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,29 @@ | ||
import { Assessment, Cycle } from 'meta/assessment' | ||
|
||
import { getKeyCycle, Keys } from 'server/repository/redis/keys' | ||
import { RedisData } from 'server/repository/redis/redisData' | ||
|
||
type Props = { | ||
assessment: Assessment | ||
cycleSource: Cycle | ||
cycleTarget: Cycle | ||
} | ||
|
||
export const renameCycleEntries = async (props: Props): Promise<void> => { | ||
const { assessment, cycleSource, cycleTarget } = props | ||
|
||
const redis = RedisData.getInstance() | ||
const sectionsKey = getKeyCycle({ assessment, cycle: cycleSource, key: Keys.Section.sections }) | ||
const sectionsKeyNew = getKeyCycle({ assessment, cycle: cycleTarget, key: Keys.Section.sections }) | ||
const sectionIndexKey = getKeyCycle({ assessment, cycle: cycleSource, key: Keys.Section.sectionsIndex }) | ||
const sectionIndexKeyNew = getKeyCycle({ assessment, cycle: cycleTarget, key: Keys.Section.sectionsIndex }) | ||
const subSectionIndexKey = getKeyCycle({ assessment, cycle: cycleSource, key: Keys.Section.subSectionsIndex }) | ||
const subSectionIndexKeyNew = getKeyCycle({ assessment, cycle: cycleTarget, key: Keys.Section.subSectionsIndex }) | ||
const sectionsMetadataKey = getKeyCycle({ assessment, cycle: cycleSource, key: Keys.Section.sectionsMetadata }) | ||
const sectionsMetadataKeyNew = getKeyCycle({ assessment, cycle: cycleTarget, key: Keys.Section.sectionsMetadata }) | ||
|
||
await redis.rename(sectionsKey, sectionsKeyNew) | ||
await redis.rename(sectionIndexKey, sectionIndexKeyNew) | ||
await redis.rename(subSectionIndexKey, subSectionIndexKeyNew) | ||
await redis.rename(sectionsMetadataKey, sectionsMetadataKeyNew) | ||
} |