Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the issues: #63

Merged
merged 3 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"dependencies": {
"@sentry/node": "^7.51.2",
"@togethercrew.dev/db": "^2.3.6",
"@togethercrew.dev/tc-messagebroker": "^0.0.36",
"@togethercrew.dev/tc-messagebroker": "^0.0.38",
"babel-jest": "^29.5.0",
"bullmq": "^3.14.0",
"discord.js": "^14.11.0",
Expand Down
9 changes: 6 additions & 3 deletions src/database/services/guild.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ async function getGuilds(filter: object): Promise<IGuild[]> {
try {
return await Guild.find(filter);
} catch (error) {
throw new Error('Failed to get guilds.');
console.log('Failed to retrieve guilds', error);
return [];
}
}

Expand All @@ -37,7 +38,8 @@ async function updateGuild(filter: object, UpdateBody: IGuildUpdateBody): Promis
Object.assign(guild, UpdateBody);
return await guild.save();
} catch (error) {
throw new Error('Failed to update guild.');
console.log('Failed to update guild', error);
return null;
}
}

Expand All @@ -53,7 +55,8 @@ async function updateManyGuilds(filter: object, UpdateBody: IGuildUpdateBody): P
const modifiedCount = updateResult.modifiedCount;
return modifiedCount;
} catch (error) {
throw new Error('Failed to update guilds.');
console.log('Failed to update guilds', error);
return 0;
}
}

Expand Down
48 changes: 22 additions & 26 deletions src/database/services/guildMember.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ import { IGuildMember, IGuildMemberUpdateBody } from '@togethercrew.dev/db';
* Create a guild member in the database.
* @param {Connection} connection - Mongoose connection object for the database.
* @param {IGuildMember} guildMember - The guild member object to be created.
* @returns {Promise<IGuildMember>} - A promise that resolves to the created guild member object.
* @returns {Promise<IGuildMember|null>} - A promise that resolves to the created guild member object.
*/
async function createGuildMember(connection: Connection, guildMember: IGuildMember): Promise<IGuildMember> {
async function createGuildMember(connection: Connection, guildMember: IGuildMember): Promise<IGuildMember | null> {
try {
return await connection.models.GuildMember.create(guildMember);
} catch (error) {
console.log(error)
throw new Error('Failed to create guild member');
console.log('Failed to create guild member', error);
return null;
}
}

/**
* Create guild members in the database.
* @param {Connection} connection - Mongoose connection object for the database.
* @param {IGuildMember[]} guildMembers - An array of guild member objects to be created.
* @returns {Promise<IGuildMember[]>} - A promise that resolves to an array of the created guild member objects.
* @returns {Promise<IGuildMember[] | []>} - A promise that resolves to an array of the created guild member objects.
*/
async function createGuildMembers(connection: Connection, guildMembers: IGuildMember[]): Promise<IGuildMember[]> {
async function createGuildMembers(connection: Connection, guildMembers: IGuildMember[]): Promise<IGuildMember[] | []> {
try {
return await connection.models.GuildMember.insertMany(guildMembers, { ordered: false });
} catch (error) {
console.log(error)
throw new Error('Failed to create guild members');
console.log('Failed to create guild members', error);
return [];
}
}

Expand All @@ -41,23 +41,23 @@ async function getGuildMember(connection: Connection, filter: object): Promise<I
try {
return await connection.models.GuildMember.findOne(filter);
} catch (error) {
console.log(error)
throw new Error('Failed to retrieve guild member');
console.log('Failed to retrieve guild member', error);
return null;
}
}

/**
* Get guild members from the database based on the filter criteria.
* @param {Connection} connection - Mongoose connection object for the database.
* @param {object} filter - An object specifying the filter criteria to match the desired guild member entries.
* @returns {Promise<IGuildMember[]>} - A promise that resolves to an array of the matching guild member objects.
* @returns {Promise<IGuildMember[] | []>} - A promise that resolves to an array of the matching guild member objects.
*/
async function getGuildMembers(connection: Connection, filter: object): Promise<IGuildMember[]> {
async function getGuildMembers(connection: Connection, filter: object): Promise<IGuildMember[] | []> {
try {
return await connection.models.GuildMember.find(filter);
} catch (error) {
console.log(error)
throw new Error('Failed to retrieve guild members');
console.log('Failed to retrieve guild members', error);
return [];
}
}

Expand All @@ -81,9 +81,8 @@ async function updateGuildMember(
Object.assign(guildMember, UpdateBody);
return await guildMember.save();
} catch (error) {
console.log(error)

throw new Error('Failed to update guild member');
console.log('Failed to update guild member', error);
return null;
}
}

Expand All @@ -103,7 +102,8 @@ async function updateGuildMembers(
const updateResult = await connection.models.GuildMember.updateMany(filter, UpdateBody);
return updateResult.modifiedCount || 0;
} catch (error) {
throw new Error('Failed to update guild members');
console.log('Failed to update guild members', error);
return 0;
}
}

Expand All @@ -112,16 +112,14 @@ async function updateGuildMembers(
* @param {Connection} connection - Mongoose connection object for the database.
* @param {object} filter - An object specifying the filter criteria to match the desired guild member entry for deletion.
* @returns {Promise<boolean>} - A promise that resolves to true if the guild member was successfully deleted, or false otherwise.
* @throws {Error} - If there is an error while deleting the guild member.
*/
async function deleteGuildMember(connection: Connection, filter: object): Promise<boolean> {
try {
const deleteResult = await connection.models.GuildMember.deleteOne(filter);
return deleteResult.deletedCount === 1;
} catch (error) {
console.log(error)

throw new Error('Failed to delete guild member');
console.log('Failed to delete guild member', error);
return false;
}
}

Expand All @@ -130,16 +128,14 @@ async function deleteGuildMember(connection: Connection, filter: object): Promis
* @param {Connection} connection - Mongoose connection object for the database.
* @param {object} filter - An object specifying the filter criteria to match multiple guild member entries for deletion.
* @returns {Promise<number>} - A promise that resolves to the number of deleted guild member entries.
* @throws {Error} - If there is an error while deleting the guild members.
*/
async function deleteGuildMembers(connection: Connection, filter: object): Promise<number> {
try {
const deleteResult = await connection.models.GuildMember.deleteMany(filter);
return deleteResult.deletedCount;
} catch (error) {
console.log(error)

throw new Error('Failed to delete guild members');
console.log('Failed to delete guild members', error);
return 0;
}
}

Expand Down
53 changes: 26 additions & 27 deletions src/database/services/rawInfo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,29 @@ import { IRawInfo, IRawInfoUpdateBody } from '@togethercrew.dev/db';
* Create a rawInfo entry in the database.
* @param {Connection} connection - Mongoose connection object for the database.
* @param {IRawInfo} rawInfo - The rawInfo object to be created.
* @returns {Promise<IRawInfo>} - A promise that resolves to the created rawInfo object.
* @returns {Promise<IRawInfo | null>} - A promise that resolves to the created rawInfo object.
*/
async function createRawInfo(connection: Connection, rawInfo: IRawInfo): Promise<IRawInfo> {
async function createRawInfo(connection: Connection, rawInfo: IRawInfo): Promise<IRawInfo | null> {
try {
return await connection.models.RawInfo.create(rawInfo);
} catch (error) {
console.log(error);
throw new Error('Failed to create rawInfo');
console.log('Failed to create rawInfo', error);
return null;
}
}

/**
* Create multiple rawInfo entries in the database.
* @param {Connection} connection - Mongoose connection object for the database.
* @param {IRawInfo[]} rawInfos - An array of rawInfo objects to be created.
* @returns {Promise<IRawInfo[]>} - A promise that resolves to an array of the created rawInfo objects.
* @returns {Promise<IRawInfo[] | []>} - A promise that resolves to an array of the created rawInfo objects.
*/
async function createRawInfos(connection: Connection, rawInfos: IRawInfo[]): Promise<IRawInfo[]> {
async function createRawInfos(connection: Connection, rawInfos: IRawInfo[]): Promise<IRawInfo[] | []> {
try {
return await connection.models.RawInfo.insertMany(rawInfos, { ordered: false });
} catch (error) {
console.log(error);

throw new Error('Failed to create rawInfos');
console.log('Failed to create rawInfos', error);
return [];
}
}

Expand All @@ -42,23 +41,23 @@ async function getRawInfo(connection: Connection, filter: object): Promise<IRawI
try {
return await connection.models.RawInfo.findOne(filter);
} catch (error) {
console.log(error)
throw new Error('Failed to retrieve rawInfo');
console.log('Failed to retrieve rawInfo', error);
return null;
}
}

/**
* Get rawInfo entries from the database based on the filter criteria.
* @param {Connection} connection - Mongoose connection object for the database.
* @param {object} filter - An object specifying the filter criteria to match the desired rawInfo entries.
* @returns {Promise<IRawInfo[]>} - A promise that resolves to an array of the matching rawInfo objects.
* @returns {Promise<IRawInfo[] | []>} - A promise that resolves to an array of the matching rawInfo objects.
*/
async function getRawInfos(connection: Connection, filter: object): Promise<IRawInfo[]> {
async function getRawInfos(connection: Connection, filter: object): Promise<IRawInfo[] | []> {
try {
return await connection.models.RawInfo.find(filter);
} catch (error) {
console.log(error)
throw new Error('Failed to retrieve rawInfos');
console.log('Failed to retrieves rawInfo', error);
return [];
}
}

Expand All @@ -82,8 +81,8 @@ async function updateRawInfo(
Object.assign(rawInfo, UpdateBody);
return await rawInfo.save();
} catch (error) {
console.log(error)
throw new Error('Failed to update rawInfo');
console.log('Failed to update rawInfo', error);
return null;
}
}

Expand All @@ -103,8 +102,8 @@ async function updateManyRawInfo(
const updateResult = await connection.models.RawInfo.updateMany(filter, UpdateBody);
return updateResult.modifiedCount || 0;
} catch (error) {
console.log(error)
throw new Error('Failed to update rawInfos');
console.log('Failed to update rawInfos', error);
return 0;
}
}

Expand All @@ -113,14 +112,14 @@ async function updateManyRawInfo(
* @param {Connection} connection - Mongoose connection object for the database.
* @param {object} filter - An object specifying the filter criteria to match the desired rawInfo entry for deletion.
* @returns {Promise<boolean>} - A promise that resolves to true if the rawInfo entry was successfully deleted, or false otherwise.
* @throws {Error} - If there is an error while deleting the rawInfo entry.
*/
async function deleteRawInfo(connection: Connection, filter: object): Promise<boolean> {
try {
const deleteResult = await connection.models.RawInfo.deleteOne(filter);
return deleteResult.deletedCount === 1;
} catch (error) {
throw new Error('Failed to delete rawInfo');
console.log('Failed to delete rawInfo', error);
return false;
}
}

Expand All @@ -136,8 +135,8 @@ async function deleteManyRawInfo(connection: Connection, filter: object): Promis
const deleteResult = await connection.models.RawInfo.deleteMany(filter);
return deleteResult.deletedCount;
} catch (error) {
console.log(error)
throw new Error('Failed to delete rawInfos');
console.log('Failed to delete rawInfo', error);
return 0;
}
}

Expand All @@ -151,8 +150,8 @@ async function getNewestRawInfo(connection: Connection, filter: object): Promise
try {
return await connection.models.RawInfo.findOne(filter).sort({ createdDate: -1 });
} catch (error) {
console.log(error)
throw new Error('Failed to retrieve rawInfo');
console.log('Failed to retrieve NewestRawInfo', error);
return null;
}
}

Expand All @@ -166,8 +165,8 @@ async function getOldestRawInfo(connection: Connection, filter: object): Promise
try {
return await connection.models.RawInfo.findOne(filter).sort({ createdDate: 1 });
} catch (error) {
console.log(error)
throw new Error('Failed to retrieve rawInfo');
console.log('Failed to retrieve OldestRawInfo', error);
return null;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/functions/cronJon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ export default async function cronJob(client: Client) {
try {
const guilds = await guildService.getGuilds({ isDisconnected: false });
for (let i = 0; i < guilds.length; i++) {
console.log(`Cron JOB is running for ${guilds[i].guildId}:${guilds[i].name}`)
const connection = databaseService.connectionFactory(guilds[i].guildId, config.mongoose.dbURL);
await guildExtraction(connection, client, guilds[i].guildId)
await createAndStartCronJobSaga(guilds[i].guildId)
console.log(`Cron JOB is Done ${guilds[i].guildId}:${guilds[i].name}`)
}
} catch (err) {
console.log(err);
console.log('Cron job failed', err)
}
}

Expand Down
Loading