feat: update infra configs mutation added

This commit is contained in:
Mir Arif Hasan
2023-11-24 20:37:35 +06:00
parent 4e188f3c11
commit 0a10f7c654
7 changed files with 109 additions and 8 deletions

View File

@@ -8,10 +8,12 @@ import {
DATABASE_TABLE_NOT_EXIST,
INFRA_CONFIG_NOT_FOUND,
INFRA_CONFIG_NOT_LISTED,
INFRA_CONFIG_UPDATE_FAILED,
} from 'src/errors';
import { throwErr } from 'src/utils';
import { ConfigService } from '@nestjs/config';
import { stopApp } from './helper';
import { InfraConfigArgs } from './input-args';
@Injectable()
export class InfraConfigService implements OnModuleInit {
@@ -153,7 +155,40 @@ export class InfraConfigService implements OnModuleInit {
return E.right(this.cast(infraConfig));
} catch (e) {
return E.left(INFRA_CONFIG_NOT_FOUND);
return E.left(INFRA_CONFIG_UPDATE_FAILED);
}
}
/**
* Update InfraConfigs by name
* @param infraConfigs InfraConfigs to update
* @returns InfraConfig model
*/
async updateMany(infraConfigs: InfraConfigArgs[]) {
try {
await this.prisma.$transaction(async (tx) => {
const deleteCount = await tx.infraConfig.deleteMany({
where: { name: { in: infraConfigs.map((p) => p.name) } },
});
const createCount = await tx.infraConfig.createMany({
data: infraConfigs.map((p) => ({
name: p.name,
value: p.value,
})),
});
if (deleteCount.count !== createCount.count) {
throwErr(INFRA_CONFIG_UPDATE_FAILED);
}
});
stopApp();
return E.right(infraConfigs);
} catch (e) {
console.log(e);
return E.left(INFRA_CONFIG_UPDATE_FAILED);
}
}