Skip to content

Commit 03e957b

Browse files
committed
fix organization soft delete, add isDeleted flag to orgs, phases, programs, cohorts, teams, users, profiles
1 parent 9fbb702 commit 03e957b

16 files changed

+92
-44
lines changed

src/models/attendance.model.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ const AttendanceSchema = new Schema({
66
required: true,
77
},
88
phase: {
9-
type: mongoose.Types.ObjectId,
9+
type: Schema.Types.ObjectId,
1010
ref: 'Phase',
1111
required: true,
1212
},
1313
cohort: {
14-
type: mongoose.Types.ObjectId,
14+
type: Schema.Types.ObjectId,
1515
ref: 'Cohort',
1616
required: true,
1717
},
@@ -31,14 +31,14 @@ const AttendanceSchema = new Schema({
3131
},
3232
},
3333
team: {
34-
type: mongoose.Schema.Types.ObjectId,
34+
type: Schema.Types.ObjectId,
3535
ref: 'Team',
3636
required: true
3737
},
3838
trainees: [
3939
{
4040
trainee: {
41-
type: mongoose.Types.ObjectId,
41+
type: Schema.Types.ObjectId,
4242
ref: 'User',
4343
},
4444
status: [

src/models/cohort.model.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import mongoose, { Schema, Document } from 'mongoose'
22
import { User } from './user'
3-
import { PhaseInterface } from './phase.model';
43

54
export interface CohortInterface extends Document {
65
id?: string;
76
name: string;
8-
phase: PhaseInterface;
7+
phase: mongoose.Types.ObjectId;
98
coordinator: mongoose.Types.ObjectId;
109
members: mongoose.Types.ObjectId[];
1110
program: mongoose.Types.ObjectId;
@@ -62,6 +61,10 @@ const cohortSchema = new Schema(
6261
ref: 'Organization',
6362
required: true,
6463
},
64+
isDeleted: {
65+
type: Boolean,
66+
default: false
67+
}
6568
},
6669
{
6770
statics: {

src/models/event.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const Event = mongoose.model(
1010
'Event',
1111
new Schema({
1212
user: {
13-
type: mongoose.Types.ObjectId,
13+
type: Schema.Types.ObjectId,
1414
ref: 'User',
1515
required: true,
1616
},

src/models/invitation.model.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const ROLE = {
1616
}
1717
const InvitationSchema = new Schema({
1818
inviterId: {
19-
type: mongoose.Types.ObjectId,
19+
type: Schema.Types.ObjectId,
2020
ref: 'User',
2121
required: true,
2222
},

src/models/notification.model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const Notification = mongoose.model(
55
new Schema(
66
{
77
receiver: {
8-
type: mongoose.Types.ObjectId,
8+
type: Schema.Types.ObjectId,
99
ref: 'User',
1010
required: true,
1111
},
@@ -14,7 +14,7 @@ const Notification = mongoose.model(
1414
required: true,
1515
},
1616
sender: {
17-
type: mongoose.Types.ObjectId,
17+
type: Schema.Types.ObjectId,
1818
ref: 'User',
1919
required: true,
2020
},

src/models/organization.model.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import mongoose, { model, Schema, Document } from 'mongoose'
2+
import Program from './program.model'
3+
import Phase from './phase.model'
4+
import Cohort from './cohort.model'
5+
import Team from './team.model'
6+
import { User } from './user'
7+
import { Profile } from './profile.model'
28

39
export interface IOrganization extends Document{
410
id?: string,
@@ -48,5 +54,37 @@ const organizationSchema = new Schema({
4854
}
4955
})
5056

57+
organizationSchema.pre('save',async function(next){
58+
if(!this.isModified('isDeleted')){
59+
return next()
60+
}
61+
await Program.updateMany({ organization: this._id },{
62+
$set: {
63+
isDeleted: true
64+
}
65+
})
66+
await Cohort.updateMany({ organization: this._id },{
67+
$set: {
68+
isDeleted: true
69+
}
70+
})
71+
await Team.updateMany({ organization: this._id },{
72+
$set: {
73+
isDeleted: true
74+
}
75+
})
76+
await Phase.updateMany({ organization: this._id },{
77+
$set: {
78+
isDeleted: true
79+
}
80+
})
81+
await Profile.updateMany({ orgId: this._id },{
82+
$set: {
83+
isDeleted: true
84+
}
85+
})
86+
next()
87+
})
88+
5189
const Organization = model('Organization', organizationSchema)
5290
export { Organization }

src/models/phase.model.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import mongoose, { Schema } from 'mongoose';
22

33
export interface PhaseInterface {
4-
_id: mongoose.Types.ObjectId;
54
name: string;
65
description: string;
76
organization: mongoose.Types.ObjectId;
@@ -18,10 +17,14 @@ const phaseSchema = new Schema({
1817
required: true,
1918
},
2019
organization: {
21-
type: mongoose.Types.ObjectId,
20+
type: Schema.Types.ObjectId,
2221
ref: 'Organization',
2322
required: true,
2423
},
24+
isDeleted: {
25+
type: Boolean,
26+
default: false
27+
}
2528
});
2629

2730
const Phase = mongoose.model('Phase', phaseSchema);

src/models/profile.model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ const profileSchema = new Schema(
6767
resume: {
6868
type: String,
6969
},
70+
isDeleted: {
71+
type: Boolean,
72+
default: false
73+
}
7074
},
7175
{
7276
toJSON: { virtuals: true },

src/models/program.model.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ const programSchema = new Schema(
1313
default: '',
1414
},
1515
manager: {
16-
type: mongoose.Types.ObjectId,
16+
type: Schema.Types.ObjectId,
1717
ref: 'User',
1818
},
1919
organization: {
20-
type: mongoose.Types.ObjectId,
20+
type: Schema.Types.ObjectId,
2121
ref: 'Organization',
2222
required: true,
2323
},
@@ -26,6 +26,10 @@ const programSchema = new Schema(
2626
required: true,
2727
default: true,
2828
},
29+
isDeleted: {
30+
type: Boolean,
31+
default: false
32+
}
2933
},
3034
{
3135
toObject: { virtuals: true },

src/models/ratingSystem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const systemRating = mongoose.model(
2929
default: false,
3030
},
3131
organization: {
32-
type: mongoose.Types.ObjectId,
32+
type: Schema.Types.ObjectId,
3333
ref: 'Organization',
3434
required: true,
3535
},

0 commit comments

Comments
 (0)