Skip to content
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
11 changes: 4 additions & 7 deletions lib/CompetitionHandling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,12 @@ export async function AssignScoutersToCompetitionMatches(
teamId: string,
competitionId: string,
) {
const comp = await db.findObjectById<Competition>(
const comp = await db.findObjectById(
CollectionId.Competitions,
new ObjectId(competitionId),
);

const team = await db.findObject<Team>(
CollectionId.Teams,
new ObjectId(teamId),
);
const team = await db.findObject(CollectionId.Teams, new ObjectId(teamId));

if (!comp || !team) {
throw new Error("Competition or team not found");
Expand Down Expand Up @@ -99,7 +96,7 @@ export async function generateReportsForMatch(
schedule?: ScheduleMatch,
) {
if (typeof match === "string") {
const foundMatch = await db.findObjectById<Match>(
const foundMatch = await db.findObjectById(
CollectionId.Matches,
new ObjectId(match),
);
Expand All @@ -114,7 +111,7 @@ export async function generateReportsForMatch(
match.subjectiveScouter = schedule?.subjectiveScouter;

const existingReportPromises = match.reports.map((r) =>
db.findObjectById<Report>(CollectionId.Reports, new ObjectId(r)),
db.findObjectById(CollectionId.Reports, new ObjectId(r)),
);
const existingReports = await Promise.all(existingReportPromises);

Expand Down
89 changes: 25 additions & 64 deletions lib/MongoDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import CollectionId, { CollectionIdToType } from "./client/CollectionId";
import DbInterface, {
WithStringOrObjectIdId,
} from "./client/dbinterfaces/DbInterface";
import { default as BaseMongoDbInterface } from "mongo-anywhere/MongoDbInterface";

if (!process.env.MONGODB_URI) {
// Necessary to allow connections from files running outside of Next
Expand Down Expand Up @@ -40,97 +41,57 @@ export async function getDatabase(): Promise<MongoDBInterface> {
return global.interface;
}

export class MongoDBInterface implements DbInterface {
promise: Promise<MongoClient> | undefined;
client: MongoClient | undefined;
db: Db | undefined;

constructor(promise: Promise<MongoClient>) {
this.promise = promise;
}

export class MongoDBInterface
extends BaseMongoDbInterface<CollectionId, CollectionIdToType<CollectionId>>
implements DbInterface
{
async init() {
this.client = await this.promise;
this.db = this.client?.db(process.env.DB);
//@ts-ignore

const CollectionId = (await this.db
?.listCollections()
.toArray()) as CollectionId;
if (CollectionId?.length === 0) {
try {
Object.values(CollectionId).forEach(
async (collectionName) =>
await this.db?.createCollection(collectionName),
);
} catch (e) {
console.log(
"Failed to create CollectionId... (probably exist already)",
);
}
}
super.init(Object.values(CollectionId));
}

async addObject<
TId extends CollectionId,
TObj extends CollectionIdToType<TId>,
>(collection: TId, object: WithStringOrObjectIdId<TObj>): Promise<TObj> {
if (object._id && typeof object._id === "string")
object._id = new ObjectId(object._id);

const ack = await this?.db
?.collection(collection)
.insertOne(object as Document & { _id?: ObjectId });
object._id = ack?.insertedId;
return object as TObj;
return super.addObject(collection, object);
}

async deleteObjectById(collection: CollectionId, id: ObjectId) {
var query = { _id: id };
await this?.db?.collection(collection).deleteOne(query);
await this?.db?.collection(collection).deleteOne({ _id: id });
}

updateObjectById<
TId extends CollectionId,
TObj extends CollectionIdToType<TId>,
>(collection: TId, id: ObjectId, newValues: Partial<TObj>): Promise<void> {
var query = { _id: id };
var updated = { $set: newValues };
this?.db?.collection(collection).updateOne(query, updated);

return Promise.resolve();
return super.updateObjectById(collection, id, newValues);
}

async findObjectById<Type>(
collection: CollectionId,
id: ObjectId,
): Promise<Type> {
return (await this?.db
?.collection(collection)
.findOne({ _id: id })) as Type;
async findObjectById<
TId extends CollectionId,
TObj extends CollectionIdToType<TId>,
>(collection: TId, id: ObjectId): Promise<TObj> {
return super.findObjectById(collection, id);
}

async findObject<Type>(
collection: CollectionId,
query: object,
): Promise<Type> {
return (await this?.db?.collection(collection).findOne(query)) as Type;
async findObject<
TId extends CollectionId,
TObj extends CollectionIdToType<TId>,
>(collection: TId, query: object): Promise<TObj> {
return super.findObject(collection, query);
}

async findObjects<Type>(
collection: CollectionId,
query: object,
): Promise<Type[]> {
return (await this?.db
?.collection(collection)
.find(query)
.toArray()) as Type[];
async findObjects<
TId extends CollectionId,
TObj extends CollectionIdToType<TId>,
>(collection: TId, query: object): Promise<TObj[]> {
return super.findObjects(collection, query);
}

async countObjects(
collection: CollectionId,
query: object,
): Promise<number | undefined> {
return await this?.db?.collection(collection).countDocuments(query);
return super.countObjects(collection, query);
}
}
8 changes: 4 additions & 4 deletions lib/UrlResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,17 @@ export default async function UrlResolver(

try {
const promises = [
db.findObject<Team>(CollectionId.Teams, { slug: teamSlug }),
db.findObject(CollectionId.Teams, { slug: teamSlug }),
seasonSlug
? db.findObject<Season>(CollectionId.Seasons, { slug: seasonSlug })
? db.findObject(CollectionId.Seasons, { slug: seasonSlug })
: null,
competitionSlug
? db.findObject<Competition>(CollectionId.Competitions, {
? db.findObject(CollectionId.Competitions, {
slug: competitionSlug,
})
: null,
reportId
? db.findObject<Report>(CollectionId.Reports, {
? db.findObject(CollectionId.Reports, {
_id: new ObjectId(reportId),
})
: null,
Expand Down
39 changes: 12 additions & 27 deletions lib/api/AccessLevels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace AccessLevels {

const team = await (
await db
).findObjectById<Team>(CollectionId.Teams, new ObjectId(teamId));
).findObjectById(CollectionId.Teams, new ObjectId(teamId));
if (!team) {
return { authorized: false, authData: undefined };
}
Expand All @@ -92,7 +92,7 @@ namespace AccessLevels {

const team = await (
await db
).findObjectById<Team>(CollectionId.Teams, new ObjectId(teamId));
).findObjectById(CollectionId.Teams, new ObjectId(teamId));
if (!team) {
return { authorized: false, authData: undefined };
}
Expand All @@ -116,10 +116,7 @@ namespace AccessLevels {

const comp = await (
await db
).findObjectById<Competition>(
CollectionId.Competitions,
new ObjectId(compId),
);
).findObjectById(CollectionId.Competitions, new ObjectId(compId));
if (!comp) {
return { authorized: false, authData: undefined };
}
Expand Down Expand Up @@ -148,7 +145,7 @@ namespace AccessLevels {

const season = await (
await db
).findObjectById<Season>(CollectionId.Seasons, new ObjectId(seasonId));
).findObjectById(CollectionId.Seasons, new ObjectId(seasonId));
if (!season) {
return { authorized: false, authData: undefined };
}
Expand Down Expand Up @@ -177,7 +174,7 @@ namespace AccessLevels {

const match = await (
await db
).findObjectById<Match>(CollectionId.Matches, new ObjectId(matchId));
).findObjectById(CollectionId.Matches, new ObjectId(matchId));
if (!match) {
return { authorized: false, authData: undefined };
}
Expand Down Expand Up @@ -211,7 +208,7 @@ namespace AccessLevels {

const report = await (
await db
).findObjectById<Report>(CollectionId.Reports, new ObjectId(reportId));
).findObjectById(CollectionId.Reports, new ObjectId(reportId));
if (!report) {
return { authorized: false, authData: undefined };
}
Expand Down Expand Up @@ -240,10 +237,7 @@ namespace AccessLevels {

const comp = await (
await db
).findObjectById<Competition>(
CollectionId.Competitions,
new ObjectId(compId),
);
).findObjectById(CollectionId.Competitions, new ObjectId(compId));
if (!comp) {
return { authorized: false, authData: undefined };
}
Expand Down Expand Up @@ -272,7 +266,7 @@ namespace AccessLevels {

const match = await (
await db
).findObjectById<Match>(CollectionId.Matches, new ObjectId(matchId));
).findObjectById(CollectionId.Matches, new ObjectId(matchId));
if (!match) {
return { authorized: false, authData: undefined };
}
Expand Down Expand Up @@ -306,10 +300,7 @@ namespace AccessLevels {

const pitReport = await (
await db
).findObjectById<Pitreport>(
CollectionId.PitReports,
new ObjectId(pitReportId),
);
).findObjectById(CollectionId.PitReports, new ObjectId(pitReportId));
if (!pitReport) {
return { authorized: false, authData: undefined };
}
Expand Down Expand Up @@ -343,7 +334,7 @@ namespace AccessLevels {

const report = await (
await db
).findObjectById<Report>(CollectionId.Reports, new ObjectId(reportId));
).findObjectById(CollectionId.Reports, new ObjectId(reportId));
if (!report) {
return { authorized: false, authData: undefined };
}
Expand Down Expand Up @@ -372,10 +363,7 @@ namespace AccessLevels {

const report = await (
await db
).findObjectById<SubjectiveReport>(
CollectionId.SubjectiveReports,
new ObjectId(reportId),
);
).findObjectById(CollectionId.SubjectiveReports, new ObjectId(reportId));
if (!report) {
return { authorized: false, authData: undefined };
}
Expand Down Expand Up @@ -404,10 +392,7 @@ namespace AccessLevels {

const picklist = await (
await db
).findObjectById<CompPicklistGroup>(
CollectionId.Picklists,
new ObjectId(picklistId),
);
).findObjectById(CollectionId.Picklists, new ObjectId(picklistId));
if (!picklist) {
return { authorized: false, authData: undefined };
}
Expand Down
14 changes: 7 additions & 7 deletions lib/api/ApiUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ export function ownsTeam(team?: Team | null, user?: User) {
}

export function getCompFromReport(db: DbInterface, report: Report) {
return db.findObject<Competition>(CollectionId.Competitions, {
return db.findObject(CollectionId.Competitions, {
matches: report.match?.toString(),
});
}

export function getCompFromMatch(db: DbInterface, match: Match) {
return db.findObject<Competition>(CollectionId.Competitions, {
return db.findObject(CollectionId.Competitions, {
matches: match._id?.toString(),
});
}

export function getCompFromPitReport(db: DbInterface, report: Pitreport) {
return db.findObject<Competition>(CollectionId.Competitions, {
return db.findObject(CollectionId.Competitions, {
pitReports: report._id?.toString(),
});
}
Expand All @@ -57,7 +57,7 @@ export function getCompFromSubjectiveReport(
report: SubjectiveReport,
) {
return db
.findObject<Match>(CollectionId.Matches, {
.findObject(CollectionId.Matches, {
subjectiveReports: report._id?.toString(),
})
.then((match) => {
Expand All @@ -71,19 +71,19 @@ export function getCompFromPicklist(
db: DbInterface,
picklist: CompPicklistGroup,
) {
return db.findObject<Competition>(CollectionId.Competitions, {
return db.findObject(CollectionId.Competitions, {
picklist: picklist._id?.toString(),
});
}

export function getSeasonFromComp(db: DbInterface, comp: Competition) {
return db.findObject<Season>(CollectionId.Seasons, {
return db.findObject(CollectionId.Seasons, {
competitions: comp?._id?.toString(), // Specifying one value is effectively includes for arrays
});
}

export function getTeamFromSeason(db: DbInterface, season: Season) {
return db.findObject<Team>(CollectionId.Teams, {
return db.findObject(CollectionId.Teams, {
seasons: season._id?.toString(),
});
}
Expand Down
Loading
Loading