Chazal tell us (Avodah Zarah 4b) that Klal Yisrael sinned in order to teach the possibility of teshuvah for the community; David sinned in order to teach that teshuvah was possible for the individual:
א"ר יהושע בן לוי לא עשו ישראל את העגל אלא ליתן פתחון פה לבעלי תשובה שנאמר מי יתן והיה לבבם זה להם ליראה אותי כל הימים וגו' והיינו דא"ר יוחנן משום ר"ש בן יוחאי לא דוד ראוי לאותו מעשה ולא ישראל ראוין לאותו מעשה לא דוד ראוי לאותו מעשה דכתיב ולבי חלל בקרבי ולא ישראל ראוין לאותו מעשה דכתיב מי יתן והיה לבבם זה להם ליראה אותי כל הימים אלא למה עשו לומר לך שאם חטא יחיד אומרים לו כלך אצל יחיד ואם חטאו צבור אומרים (לו כלך) אצל צבור
לא עשו ישראל את העגל. כלומר גבורים ושליטים ביצרם היו ולא הי' ראוי להתגבר יצרם עליהן אלא גזירת מלך היתה לשלוט בם כדי ליתן פתחון פה לבעלי תשובה שאם יאמר החוטא לא אשוב שלא יקבלני אומרים לו צא ולמד ממעשה העגל שכפרו ונתקבלו בתשובה:
Maharal has problems with the idea of גזירת מלך היתה לשלוט בם. Did G-d decree that they would sin? Was there no bechira!? Maharal (in Tiferes Yisrael) explains that what the gemara means is that Klal Yisrael and David haMelech were on such high levels that they should have been afforded extra protection from the yetzer hara. Torah protects a person from sin and doing mitzvos protect a person from sin. However, when it came to cheit ha'eigel and the episode with Bas Sheva, Hashem did not intervene and afford that extra protection. Klal Yisrael and David where left to battle the yetzer hara without any defenses to help them.
Even though there are countless pesukim in navi exhorting Klal Yisrael to do teshuvah, we still need these exemplars of teshuva because a person has the ability to explain away any and all proofs to justify avoiding changing their behavior. Whatever pasuk you might quote, whatever the navi might say over in the name of Hashem, the sinner will reinterpret and (mis)interpret to mean something else. The yetzer hara that fights against teshuvah corrupts not just a person's actions, but it corrupts a person's thinking and a person's ruchniyus, to the point that like Acheir, he might even hear a bas kol telling him that teshuvah is not possible. Meaning, the very religiosity that should inspire a person to teshuvah can be used by the yetzer hara to undermine teshuvah, telling a person that his sins have doomed him and any proofs to the contrary are just false hope, a mirage. Comes David haMelech, comes the episode of the eigel, and they show us concrete proof, maaseh rav, to the contrary. (see Michtav m'Eliyahu vol 5 p 248)
This is the background and context to Moshe's argument to Hashem that if he does not forgive Klal Yisrael, וְאִם־אַ֕יִן מְחֵ֣נִי נָ֔א מִֽסִּפְרְךָ֖ אֲשֶׁ֥ר כָּתָֽבְתָּ(32:32)
The gemara writes that before R"H there are three books that are opened. G-d inscribes the perfect tzadikim into the Book of Life and signs it. G-d inscribes the wicked into the opposite book and signs their decree. Those in the middle are written into the Book of Beinonim and their judgment is suspended until Y"K -- no sign off on anything yet.
Why does there have to be a book for the folks in the middle? Since they will eventually share either the same lot as the tzadikim or as the reshaim, let them not be written anywhere until their fate is determined?
"B'makom she'baalei teshuvah omdim afilu tzadikim gemurim einam yecholim la'amod." The answer is that the Book of Beinonim is not the same as the book in which the tzadikim are inscribed -- it is greater. To be written in that book and then take advantage of the opportunity to do teshuvah is the highest level possible.
Maor vaShemesh explains that Moshe Rabeinu threw his lot in with the Jewish people and said to G-d that the whole point of this episode of the eigel, the whole point of a distinct book for Beinonim "asher kasavta," that you write but don't seal and sign off on, is to teach the greatness and power of teshuvah. If you won't forgive, if you don't want to prove that repentance is not only possible, but can raise a person to the greatest heights, then erase me from that book, because it has no value and is unnecessary.