restore archived class student on invite

This commit is contained in:
Thibault Duplessis 2020-04-26 10:27:12 -06:00
parent c8763c9fb0
commit fa13789818
2 changed files with 15 additions and 13 deletions

View file

@ -360,7 +360,7 @@ final class Clas(
def studentArchive(id: String, username: String, v: Boolean) = Secure(_.Teacher) { _ => me =>
WithClass(me, id) { clas =>
WithStudent(clas, username) { s =>
env.clas.api.student.archive(s.student, me, v) inject
env.clas.api.student.archive(s.student.id, me, v) inject
Redirect(routes.Clas.studentShow(clas.id.value, username)).flashSuccess
}
}

View file

@ -199,11 +199,12 @@ final class ClasApi(
}
}
def invite(clas: Clas, user: User, realName: String, teacher: User): Fu[Option[Student]] = {
lila.mon.clas.studentInvite(teacher.id)
val student = Student.make(user, clas, teacher.id, realName, managed = false)
coll.insert.one(student) >> sendWelcomeMessage(teacher, user, clas) inject student.some
}.recover(lila.db.recoverDuplicateKey(_ => none))
def invite(clas: Clas, user: User, realName: String, teacher: User): Fu[Option[Student]] =
archive(Student.id(user.id, clas.id), user, false) orElse {
lila.mon.clas.studentInvite(teacher.id)
val student = Student.make(user, clas, teacher.id, realName, managed = false)
coll.insert.one(student) >> sendWelcomeMessage(teacher, user, clas) inject student.some
}.recover(lila.db.recoverDuplicateKey(_ => none))
private[ClasApi] def join(clas: Clas, user: User, teacherId: User.ID): Fu[Student] = {
val student = Student.make(user, clas, teacherId, "", managed = false)
@ -215,14 +216,15 @@ final class ClasApi(
authenticator.setPassword(s.userId, password) inject password
}
def archive(s: Student, t: User, v: Boolean): Funit =
coll.update
.one(
$id(s.id),
if (v) $set("archived" -> Clas.Recorded(t.id, DateTime.now))
else $unset("archived")
def archive(sId: Student.Id, t: User, v: Boolean): Fu[Option[Student]] =
coll.ext
.findAndUpdate[Student](
selector = $id(sId) ++ $doc("archived" $exists !v),
update =
if (v) $set("archived" -> Clas.Recorded(t.id, DateTime.now))
else $unset("archived"),
fetchNewObject = true
)
.void
def allIds = idsCache.getUnit