Merge pull request 'feat: add clear couch transaction API' (#131) from development into production
Reviewed-on: #131production 1.0.0-cloud.1
commit
0b482be669
|
@ -19,6 +19,7 @@ export class CouchService {
|
|||
}
|
||||
|
||||
async onModuleInit() {
|
||||
// return;
|
||||
const nano = this.nanoInstance;
|
||||
for (const database of DatabaseListen) {
|
||||
const db = nano.db.use(database);
|
||||
|
@ -95,4 +96,41 @@ export class CouchService {
|
|||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
getUnixTimestampLast7Days() {
|
||||
const date = new Date();
|
||||
date.setDate(date.getDate() - 4);
|
||||
date.setHours(0, 0, 0, 0);
|
||||
return date.getTime();
|
||||
}
|
||||
|
||||
public async clearTransactions() {
|
||||
const nano = this.nanoInstance;
|
||||
const transaction = nano.use('transaction');
|
||||
|
||||
const expiredDate = this.getUnixTimestampLast7Days();
|
||||
|
||||
const selectorPayment = {
|
||||
created_at: {
|
||||
$lt: expiredDate,
|
||||
},
|
||||
};
|
||||
|
||||
const transactions = await transaction.find({
|
||||
selector: selectorPayment,
|
||||
fields: ['_id', '_rev'],
|
||||
limit: 100000,
|
||||
});
|
||||
|
||||
const { docs } = transactions;
|
||||
console.log(docs.length);
|
||||
const deletedDocs = {
|
||||
docs: docs.map((doc) => ({
|
||||
_id: doc._id,
|
||||
_rev: doc._rev,
|
||||
_deleted: true,
|
||||
})),
|
||||
};
|
||||
await transaction.bulk(deletedDocs);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,13 +5,17 @@ import { Public } from 'src/core/guards';
|
|||
import * as Nano from 'nano';
|
||||
import { CreateUserPrivilegeDto } from 'src/modules/user-related/user-privilege/infrastructure/dto/create-user-privilege.dto';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { CouchService } from '../data/services/couch.service';
|
||||
|
||||
@ApiTags(`couch`)
|
||||
@Controller('v1/couch')
|
||||
@Public()
|
||||
@Injectable()
|
||||
export class CouchDataController {
|
||||
constructor(private configService: ConfigService) {}
|
||||
constructor(
|
||||
private configService: ConfigService,
|
||||
private couchService: CouchService,
|
||||
) {}
|
||||
|
||||
get nanoInstance() {
|
||||
const couchConfiguration = this.configService.get<string>('COUCHDB_CONFIG');
|
||||
|
@ -64,4 +68,11 @@ export class CouchDataController {
|
|||
// return people.get();
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
@Public(true)
|
||||
@Get('clear-transactions')
|
||||
async clearTransactions(): Promise<string> {
|
||||
await this.couchService.clearTransactions();
|
||||
return 'OK';
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue